Python 파이썬

Google selenium Crawling Python 특정 이미지 크롤링

Managemnet S/W saehyeong.woo 2022. 8. 25. 14:51
SMALL

많은양의 다양하고 같은 종류의 이미지를 모으려면 손으로 직접 다운받으면 굉장히 힘들다

그렇기에 구글에서 제공하는 툴로 원하는 이미지를 크롤링 가능하다.

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


driver = webdriver.Chrome()
elem = driver.find_element_by_name("q")
elem.send_keys("usb")
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_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(3)
        imgUrl=driver.find_element_by_css_selector(".n3VNCb").get_attribute("src")
        urllib.request.urlretrieve(imgUrl, str(count)+"usb.jpg")
        count = count+1
    except:
        pass


driver.close()

Crawling 이란?

-> Web에서는 돌아다니면서 ㄷ원하는 정보를 수집하는 행위를 의미

 

통합 개발환경 (IDE)

-> Python selenium

-> Visual Studio Code 1.62.3 version

-> Chrome Driver 96.0.4664.45 (64bit)

실행 화면

 

결과

 

찾고자 하는 USB Type 별로 총 4172장이 저장된것을 확인할 수 있다.

LIST