-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScraperImages.py
72 lines (63 loc) · 2.36 KB
/
ScraperImages.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import bs4
import requests
import shutil
import os
data = input('Search Images:')
size = int(input('How Much Images: '))
while True:
choice = input("What Search Engine do you want to use?\n 1) Google\n 2) Bing\n > Answer(1 or 2): ")
if choice.lower() == "1" or choice.lower() == 'GOOGLE_IMAGE':
GOOGLE_IMAGE = \
'https://www.google.com/search?site=&tbm=isch&source=hp&biw=1873&bih=990&'
break
elif choice.lower() == "2" or choice.lower() == 'BING_IMAGE':
BING_IMAGE = \
'https://bing.com/'
break
else:
print("\n Wrong input, try again", 'red')
#time.sleep(3)
#clrscr()
continue
def extract(data, size):
while True:
ans = choice
if ans == '1':
URL_input = GOOGLE_IMAGE + 'q=' + data
print('Fetching from url =', URL_input)
break
elif ans == '2':
URL_input = BING_IMAGE + 'q=' + data
print('Fetching from url =', URL_input)
continue
URLdata = requests.get(URL_input)
soup = bs4.BeautifulSoup(URLdata.text, "html.parser")
img = soup.find_all('img')
i = 0
print('Please wait..')
while i < size:
for link in img:
try:
images = link.get('src')
ext = images[images.rindex('.'):]
if ext.startswith('.png'):
ext = '.png'
elif ext.startswith('.jpg'):
ext = '.jpg'
elif ext.startswith('.jfif'):
ext = '.jfif'
elif ext.startswith('.com'):
ext = '.jpg'
elif ext.startswith('.svg'):
ext = '.svg'
data = requests.get(images, stream=True)
filename = "Downloads/"+str(i) + ext
with open(filename, 'wb') as file:
shutil.copyfileobj(data.raw, file)
i += 1
except:
pass
print('\t\t\t Downloaded Successfull\t\t ')
extract(data, size)
#By palahsu
#NOTE: Some code lines have been modified.