크롬 드라이브 다운로드

https://chromedriver.chromium.org/downloads

 

크롬 드라이브의 압축파일을 실행 파일과 동일한 위치에 두고 압축을 풀어준다.

그리고 코드 실행

 

from selenium import webdriver
from urllib.request import urlopen
from selenium.webdriver.common.keys import Keys
import time
import urllib.request
import os

search = "검색어"

if not os.path.isdir(search + "/"):
    os.makedirs(search + "/")

driver = webdriver.Chrome()
driver.get("https://www.google.co.kr/imghp?hl=ko&ogbl")

elem = driver.find_element_by_name("q")
elem.send_keys(search)
elem.send_keys(Keys.RETURN)

SCROLL_PAUSE_TIME = 1

last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(SCROLL_PAUSE_TIME)
    new_height = driver.execute_script("return document.body.scrollHeight")

    if new_height == last_height:
        try:
            driver.find_element_by_css_selector(".mye4qd").click()
        except:
            break
    last_height = new_height

images = driver.find_elements_by_css_selector(".rg_i.Q4LuWd")
count = 1

for image in images:
    try:
        image.click()
        time.sleep(2)
        imgUrl = driver.find_element_by_xpath("//*[@id='Sva75c']/div/div/div[3]/div[2]/c-wiz/div/div[1]/div[1]/div[2]/div/a/img").get_attribute('src')
        urllib.request.urlretrieve(imgUrl, search + "/" + search + "_" + str(count) + ".jpg")
        print("Image saved: {}_{}.jpg".format(search, count))
        count += 1
    except:
        pass
        
print("Crawling End")
driver.close()

'Language > Python' 카테고리의 다른 글

json 파일 줄 맞춰 정리하기  (0) 2022.04.26
rec to image  (0) 2022.04.07
Python에서 Void Pointer 사용하기  (0) 2022.03.22
이미지 경로 리스트 만들기  (0) 2022.01.28
원하는 크기의 이미지 resize (padding 붙이기!)  (0) 2022.01.27

+ Recent posts