-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogle Image Download.py
More file actions
executable file
·44 lines (41 loc) · 1.24 KB
/
Google Image Download.py
File metadata and controls
executable file
·44 lines (41 loc) · 1.24 KB
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
import urlparse2
import html2text
from bs4 import BeautifulSoup
import urllib
import urllib.request
import re
import os
import csv
import re
import requests
search = input('Enter anything to download the related images : ')
n = int(input('Enter number of Images to download : '))
url = 'https://www.google.com/search?newwindow=1&safe=off&espv=2&biw=828&bih=667&tbm=isch&sa=1&q=' + search.replace(' ','+') + '&oq=' + search.replace(' ','+')
#data = urllib.request.urlopen(url)
req = urllib.request.Request(
url,
data=None,
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
}
)
f = urllib.request.urlopen(req)
#print(f.read().decode('utf-8'))
soup = BeautifulSoup(f,'html.parser')
ct = 0
for i in soup.findAll('a'):
try:
raw = str(i.get('href'))
if 'imgurl' not in raw:
continue
tmp = raw.find('h')
tmp2 = raw.find('&')
img_url = raw[tmp:tmp2]
name = img_url.split('/')[-1]
urllib.request.urlretrieve(img_url,name)
ct+=1
print(str(ct) + ' Image Downloaded..')
except:
pass
if ct ==n:
break