본문 바로가기
Programming language/Python

selenium을 이용한 crawler

by jino22 2022. 2. 8.

틀린 부분이 있다면 언제든지 댓글 남겨주세요! 


selenium

: firefox, internet explorer, chrome 등과 같은 브라우저를 컨트롤.

python 3.6이상부터 pip로 library 설치 가능, 각 브라우저에 맞는 driver가 필요하다

* chrome을 사용하는 경우 chromedriver.exe 필요 https://sites.google.com/chromium.org/driver/

 

ChromeDriver - WebDriver for Chrome

WebDriver is an open source tool for automated testing of webapps across many browsers. It provides capabilities for navigating to web pages, user input, JavaScript execution, and more. ChromeDriver is a standalone server that implements the W3C WebDriver

sites.google.com

드라이버 다운
chrome 업데이트 필수!

파일을 다운로드하고 코드 파일과 같은 경로에 넣어두면 크롤링을 위한 준비가 끝난다.

다음의 코드로 크롬드라이버를 구동시켜 이용하면 된다.

from selenium import webdriver

driver=webdriver.Chrome("./chromedriver.exe")

 

!! 하지만, 이 때 크롬의 버전과 chromedriver의 버전이 맞지 않으면 오류가 생길 수 있다.

그래서 내 크롬 버전에 맞게 알아서 드라이버 버전을 다운해주는 라이브러리가 있다.

자동 다운로드 라이브러리

라이브러리 설치 후 다음과 같은 코드로 이용하면 된다.

from selenium import webdriver
import chromedriver_autoinstaller

chrome_ver = chromedriver_autoinstaller.get_chrome_version().split('.')[0]

try:
    driver=webdriver.Chrome(f"./{chrome_ver}./chromedriver.exe")
except:
    chromedriver_autoinstaller.install(True)
    driver = webdriver.Chrome(f'./{chrome_ver}/chromedriver.exe')

 

728x90
반응형

'Programming language > Python' 카테고리의 다른 글

PyQt5  (0) 2023.05.23
COS Pro 2급 python 1차  (0) 2022.05.23
[CodingStudy] Beakjoon 6588번 골드 바흐의 추측  (0) 2022.01.02
[CodingStudy] Heap -더 맵게  (0) 2021.11.22
[CodingStudy] BFS-게임맵 최단거리  (0) 2021.11.09

댓글