-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawling.py
45 lines (37 loc) · 1.33 KB
/
crawling.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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import urllib.request
driver = webdriver.Chrome()
driver.get("https://www.google.co.kr/imghp?hl=ko")
elem = driver.find_element_by_name("q")
elem.send_keys("touching the mask")
elem.send_keys(Keys.RETURN)
SCROLL_PAUSE_TIME =1
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
try:
driver.find_element_by_class_name(".mye4qd").click()
except:
break
last_height = new_height
imges=driver.find_elements_by_css_selector(".rg_i.Q4LuWd")
count=1
for image in imges:
try:
image.click()
time.sleep(2)
imgUrl=driver.find_element_by_xpath("/html/body/div[2]/c-wiz/div[3]/div[2]/div[3]/div/div/div[3]/div[2]/c-wiz/div/div[1]/div[1]/div[2]/div[1]/a/img").get_attribute("src")
urllib.request.urlretrieve(imgUrl,str(count)+".jpg")
count=count+1
except:
pass
driver.close()