-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogle_Image_Scrapper.py
48 lines (36 loc) · 1.24 KB
/
Google_Image_Scrapper.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
# %% [markdown]
# ## Python Google image scrapper
# Python program to get the images from the google based on the query search -
#
# - This program can be used to collect the `Train & Test data` for the `Neural Network`
# %%
from selenium import webdriver
from bs4 import BeautifulSoup
import os
import base64
# %%
# Getting the chrome web driver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
searchitem = str(input("Enter the search query")).strip()
searchitem.replace(" ", "+")
driver.get(
"https://www.google.com/search?q=" + searchitem + "&source=lnms&tbm=isch&sa=X"
)
content = driver.page_source
soup = BeautifulSoup(content)
filename_number = 0
os.chdir(os.getcwd())
os.mkdir(searchitem)
os.chdir(os.getcwd() + "/" + searchitem)
for a in soup.findAll("img", attrs={"class": "rg_i Q4LuWd"}):
try:
decodedImage = base64.b64decode(a.get_attribute_list(key="src")[0][23:])
img_file = open( searchitem + str(filename_number) +".jpeg", "wb")
img_file.write(decodedImage)
img_file.close()
filename_number += 1
except:
pass
print("Action completed.")