forked from ankitjain28may/ExtractGoogleImages
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
40 lines (33 loc) · 1.29 KB
/
main.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
from bs4 import BeautifulSoup
import requests
import urllib.request
import json
def get_soup(url,header):
return BeautifulSoup(urllib.request.urlopen(urllib.request.Request(url,headers=header)),'html.parser')
query = input("Enter Image Query\n")# you can change the query for the image here
image_type="ActiOn"
query= query.split()
query='+'.join(query)
url="https://www.google.co.in/search?q="+query+"&source=lnms&tbm=isch"
print(url)
#add the directory for your image here
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"
}
soup = get_soup(url,header)
ActualImages=[]# contains the link for Large original images, type of image
for a in soup.find_all("div",{"class":"rg_meta"}):
link , Type =json.loads(a.text)["ou"] ,json.loads(a.text)["ity"]
ActualImages.append((link,Type))
print("there are total" , len(ActualImages),"images")
#Checks for problems with retrieving the images in the query
for i ,(img,Type) in enumerate(ActualImages):
try:
if len(Type)==0:
Type='jpg'
path=str(i)+"."+Type
print("Please Wait, Downloading..."+str(i))
urllib.request.urlretrieve(img, "ho/"+path)
#prints error message
except Exception as error:
print(error)
input(img)