-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogleImages-headless.py
66 lines (60 loc) · 1.85 KB
/
googleImages-headless.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
import os
import requests
import shutil
import time
import pyautogui
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
baseURL = "https://www.google.com/search?tbm=isch&biw=1536&bih=722&ei=7JaAXsX1MYvUUebfvPgH&q="
query = "coding memes jpg"
url = baseURL + query
chrome_options = Options()
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920,1200")
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(options=chrome_options)
driver.get(url)
html = driver.execute_script("return document.documentElement.outerHTML")
sel_soup = BeautifulSoup(html, 'html.parser')
print(len(sel_soup.findAll('img')))
images = []
for image in sel_soup.findAll('img'):
#print(image)
try:
src = image["src"]
except:
src = image["data-src"]
images.append(src)
print(len(images))
currentPath = os.getcwd()
j = 0
for image in images:
#print(image)
fileName = os.path.basename(str(j)+".jpg")
#print(fileName)
filePath = os.path.join(currentPath, "images", fileName)
#print(filePath)
#print(image)
try:
imageRequest = requests.get(image, stream=True)
with open(filePath, "wb") as outputFile:
shutil.copyfileobj(imageRequest.raw, outputFile)
print("Done")
del imageRequest
j += 1
except:
driver2 = webdriver.Chrome()
driver2.get(image)
pyautogui.hotkey('ctrl', 's')
time.sleep(1)
pyautogui.typewrite(str(j) + '.html')
pyautogui.hotkey('enter')
driver2.close()
j += 1
pass
driver.quit()