본문 바로가기

크롤링

colab에서 selenium 사용하는 법

colab에서 selenium 사용하는 법

구글 Colab과 같이 클라우드 형태의 개발환경에서는 selenium을 이용한 크롬드라이버를 바로 사용할 수 없음

별도로 설치를 하여야함

설치 후, 사용할 경우에도 크롤드라이버 옵션들을 설정해 주어야 사용이 가능함

!pip install selenium
!apt-get update
!apt install chromium-chromedriver

from selenium import webdriver
from urllib.request import urlopen
from bs4 import BeautifulSoup as bs
from urllib.parse import quote_plus
from selenium.webdriver.common.keys import Keys
import time

url = 'https://search.naver.com/search.naver?where=image&sm=tab_jum&query='
kword = input('검색어를 입력하세요 : ')
base_url = url + quote_plus(kword)
base_url

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver', chrome_options=chrome_options)
driver.get(base_url)

body = driver.find_element_by_css_selector('body')

# 페이지 다운시켜서 더 많은 이미지가 나오게 한다
for i in range(20):
    body.send_keys(Keys.PAGE_DOWN)
    time.sleep(1)

imgs = driver.find_elements_by_css_selector('img._img')

for idx, img in enumerate(imgs):
    # print(idx,img.get_attribute('src'))
    imgUrl = img.get_attribute('src')
    imgName = '/content/drive/My Drive/Colab Notebooks/crawling/'+kword + str(idx)+'.jpg'
    urllib.request.urlretrieve(imgUrl, imgName)