-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
52 lines (45 loc) · 1.47 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
41
42
43
44
45
46
47
48
49
50
51
52
'''
Author - Aditya Mangal
Date - 27 october 2020
Purpose - Python mini task
'''
import bs4
import requests
import shutil
import os
GOOGLE_IMAGE = \
'https://www.google.com/search?site=&tbm=isch&source=hp&biw=1873&bih=990&'
def extract(data, quantity):
URL_input = GOOGLE_IMAGE + 'q=' + data
print('Fetching from url =', URL_input)
URLdata = requests.get(URL_input)
soup = bs4.BeautifulSoup(URLdata.text, "html.parser")
ImgTags = soup.find_all('img')
i = 0
print('Please wait..')
while i < quantity:
for link in ImgTags:
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 = str(i) + ext
with open(filename, 'wb') as file:
shutil.copyfileobj(data.raw, file)
i += 1
except:
pass
print('\t\t\t Downloaded Successfully..\t\t ')
data = input('What are you looking for? ')
quantity = int(input('How many photos you want? '))
extract(data, quantity)