셋팅
1) selenium 설치
Anaconda3 > Anaconda Prompt > conda install selenium 또는
cmd > cd C:\Users\Anaconda3\Scripts > pip install selenium 또는
에디터 내의 라이브러리 확장 프로그램을 활용해 selenium을 설치하면 된다
2) 브라우저 드라이버 다운로드
https://www.seleniumhq.org/download > 개발환경에 맞는 드라이버 다운

3) 드라이버 실행하기
from selenium import webdriver
driver = webdriver.Chrome('./chrome/chromedriver') #드라이브를 저장한 폴더 위치
if driver == None:
sys.exit()
driver.get("http://www.naver.com")
활용
-동적 데이터를 추출하는데 강하다
-브라우저 캡쳐 이미지 저장하기
import os
os.system("mkdir images")
driver.save_screenshot("./images/naver.jpg")
-브라우저 동작 제어하기(검색창에 "토스 스타벅스 공짜" 검색해서 해당 페이지로 이동)
elem_search = driver.find_element_by_id("query")
elem_search.clear()
elem_search.send_keys("토스 스타벅스 공짜")
xpath = """//*[@id="search_btn"]""" #F12 > 검색버튼 영역의 CSS Selector Copy
elem_search_btn = driver.find_element_by_xpath(xpath)
elem_search_btn.click()
-페이지 소스 가져오기
from bs4 import BeautifulSoup
html = driver.page_source # 현재 페이지 정보를 가져옴
print(html)
soup = BeautifulSoup(html, 'html.parser')
-더 자세한 내용은 아래 사이트에서 참고 가능하다
http://selenium-python.readthedocs.io/index.html
http://docs.seleniumhq.org/docs/
'Programming > Python * Django' 카테고리의 다른 글
[Django] runserver 수행시 broken pipe 이슈 해결 (0) | 2021.07.05 |
---|---|
[Django] ORM 구조와 원리 그리고 최적화 전략 - Pycon2020 김성렬님 발표 정리 (0) | 2021.06.06 |
[Python] 정규표현식(Regular Expression) 기본 문법, 파이썬 re 모듈 활용 (0) | 2019.05.24 |
[Python] 파이썬 SQLite 연동하기 (0) | 2019.05.21 |