forked from DedSecInside/Awesome-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape_images.py
52 lines (40 loc) · 1.59 KB
/
scrape_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import json
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen, urlretrieve
def save(url, filename):
"""
Save the given url.
Args:
url: (str): write your description
filename: (str): write your description
"""
# This function inputs the image link and the name with which
# the image needs to be saved, and thus downloads that image
urlretrieve(url, filename)
def scrape(q):
"""
Scrape the given query
Args:
q: (str): write your description
"""
# This function scrapes the google images for the particular keyword
# and saves the first picture related to it.
url = "https://www.google.com/search?q={}&source=lnms&tbm=isch".format(q)
# default header for any browser
header={'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) \
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"
}
# instantiating a bs4 object
soup = BeautifulSoup(urlopen(Request(url,headers=header)),'html.parser')
# finding all links and storing the first one
# we can download all images by iterating over the list
# the links of each image are present inside a div with the class: rg_meta notranslate
a = soup.find_all("div", {"class": "rg_meta notranslate"})[0]
link = json.loads(a.text)["ou"]
# downloads the photo
save(link, "{}.jpg".format(q.replace('+', ' ')))
print('Image of {} downloaded.'.format(q.replace('+', ' ')))
if __name__ == '__main__':
search = input("Enter a keyword: ").split()
search = '+'.join(search)
scrape(search)