-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler_app.py
60 lines (46 loc) · 1.94 KB
/
crawler_app.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
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import urllib.request
import os
def makedirs(path):
try:
os.makedirs(path)
except OSError:
if not os.path.isdir(path):
raise
print("hello world!")
search=input("What crawled do you want?\t")
number=int(input(f"How many crawled {search}s do you want?\t"))
foldername='./Crawled_image/'+search+'/'
filename=search+'_image'
interval=0.5
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("--single-process")
chrome_options.add_argument("--disable-dev-shm-usage")
path='./chromedriver'
driver = webdriver.Chrome(path, chrome_options=chrome_options)
driver.get(f'https://www.google.com/search?q=a+{search}&tbm=isch&tbs=il:ol&rlz=1C1CHZN_koKR1016KR1016&hl=ko&sa=X&ved=0CAAQ1vwEahcKEwjw-_SOuI_7AhUAAAAAHQAAAAAQAg&biw=941&bih=942')
firstImage=driver.find_element(By.CSS_SELECTOR,'#islrg > div.islrc > div:nth-child(2) > a.wXeWr.islib.nfEiy > div.bRMDJf.islir > img')
firstImage.click()
makedirs(foldername)
for i in range(number):
try:
time.sleep(interval)
image=driver.find_element(By.CSS_SELECTOR,'#Sva75c > div > div > div.pxAole > div.tvh9oe.BIB1wf > c-wiz > div > div.OUZ5W > div.zjoqD > div.qdnLaf.isv-id.b0vFpe > div > a > img')
imageSrc=image.get_attribute('src')
such_filename=f'{filename}_{i+1}.jpg'
image_path = foldername + such_filename
#print(image_path)
urllib.request.urlretrieve(imageSrc,image_path)
except:
print(such_filename+'\t\t실패')
else:
print(such_filename+'\t\t저장')
finally:
nextButton=driver.find_element(By.CSS_SELECTOR,'#Sva75c > div > div > div.pxAole > div.tvh9oe.BIB1wf > c-wiz > div > div.OUZ5W > div.zjoqD > div.mWagE.fDqwl > a:nth-child(4)')
nextButton.click()
driver.quit()
print("bye world!")