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)
'크롤링' 카테고리의 다른 글
동행복권 Open Api 코드 (0) | 2020.10.06 |
---|---|
로또 번호 추출 코드 예제 (0) | 2020.10.06 |
네이버에서 고양이 이미지 가져오기 코드 (0) | 2020.10.05 |
울산 코로나 환자 현황 크롤링 (0) | 2020.09.29 |
네이버 금융 가격 가져오기 샘플 (0) | 2020.09.28 |