happyso
study with happyso
happyso
전체 방문자
오늘
어제
  • 분류 전체보기 (300)
    • GIT (3)
    • 컴퓨터 기본 개념 (29)
    • 알고리즘 (125)
      • 알고리즘 문제 (115)
      • 알고리즘 개념 (10)
    • Go (2)
    • 클라우드 (53)
      • DevOps (3)
      • Kubernetes(쿠버네티스) (33)
      • AWS (6)
      • CKA (8)
    • 리눅스(Linux) (18)
      • 컨테이너(Container) (8)
    • Front (22)
      • JavaScript (2)
      • React (20)
    • Python (21)
      • Python 웹 크롤링 (11)
      • Django (7)
      • MachineLearning (3)
    • 데이터베이스 (6)
      • MariaDB (2)
      • MongoDB (4)
    • C언어 (5)
    • Trouble Shooting (2)
    • 네트워크 (8)
      • CCNA (5)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 18
  • kubernetes
  • 15
  • apply
  • replace
  • edit
  • Patch

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
happyso

study with happyso

Selenium
Python/Python 웹 크롤링

Selenium

2020. 7. 29. 10:51

1. Selenium이란?

https://selenium-python.readthedocs.io/

 

Selenium with Python — Selenium Python Bindings 2 documentation

Note This is not an official documentation. If you would like to contribute to this documentation, you can fork this project in Github and send pull requests. You can also send your feedback to my email: baiju.m.mail AT gmail DOT com. So far 40+ community

selenium-python.readthedocs.io

- 프로그램 실행하면 브라우저 자동으로 띄워줌(웹드라이버를 통해 제공)

- 편하지만 느림

- 실제 브라우저로 실행한 것과 동일한 값을 얻을 수 있다.

- 브라우저를 직접 동작시키기 떄문에 Javascript등 비동기적으로 혹은 뒤늦게 로드되는 컨텐츠들을 가져올 수 있다.

- 즉, 눈에 보이는 모든 컨텐츠를 다 가져올 수 있다.

- chrome들의 브라우저를 제어

 

 

 

2. Selenium 설치

- selenium 설치되어있는지 확인

- 설치 명령어

- 설치 확인

 

2. selenium을 통해 자동으로 https://www.python.org/ 페이지를 열고 해당 페이지 안에 있는 pypi버튼을 클릭해 https://pypi.org/가 열리도록 구현해본다.

- 페이지 URL

https://www.python.org/

 

Welcome to Python.org

The official home of the Python Programming Language

www.python.org

https://pypi.org/

 

PyPI · The Python Package Index

The Python Package Index (PyPI) is a repository of software for the Python programming language.

pypi.org

- Webdriver생성 후 실행하니 빈 페이지가 띄워짐

from selenium import webdriver
import time

# Webdriver 객체생성 : chromedriver.exe 를 실행
browser = webdriver.Chrome('./chromedriver.exe')

 

 

- 요청하고자 하는 url을 적은 후 다시 실행하니 해당 페이지가 자동으로 띄워짐

from selenium import webdriver
import time

# Webdriver 객체생성 : chromedriver.exe 를 실행
browser = webdriver.Chrome('./chromedriver.exe')

# python.org 요청을 보내기
browser.get('https://www.python.org/')

 

 

- 해당 페이지 3초간 떠있다 자동으로 사라짐

from selenium import webdriver
import time

# Webdriver 객체생성 : chromedriver.exe 를 실행
browser = webdriver.Chrome('./chromedriver.exe')
browser
# python.org 요청을 보내기
browser.get('https://www.python.org/')
# pypi 에 해당하는 element 찾기
# click() 함수 실행
# sleep
time.sleep(3)
# driver종료
browser.quit()

 

 

- python.org페이지를 자동으로 열고 자동으로 pypi탭을클릭한후 닫힌다.

from selenium import webdriver
import time

# Webdriver 객체생성 : chromedriver.exe 를 실행
browser = webdriver.Chrome('./chromedriver.exe')
browser
# python.org 요청을 보내기
browser.get('https://www.python.org/')
# pypi 에 해당하는 element 찾기
menus = browser.find_elements_by_css_selector('#top ul.menu li')
#print(menus) #[<selenium.webdriver.remote.webelement.WebElement (session="f68a9f0d3dea9910d1082f547c75d945", element="0.18112714160758459-1")>, <selenium.webdriver.remote.webelement.WebElement (session="f68a9f0d3dea9910d1082f547c75d945", element="0.18112714160758459-2")>, <selenium.webdriver.remote.webelement.WebElement (session="f68a9f0d3dea9910d1082f547c75d945", element="0.18112714160758459-3")>, <selenium.webdriver.remote.webelement.WebElement (session="f68a9f0d3dea9910d1082f547c75d945", element="0.18112714160758459-4")>, <selenium.webdriver.remote.webelement.WebElement (session="f68a9f0d3dea9910d1082f547c75d945", element="0.18112714160758459-5")>, <selenium.webdriver.remote.webelement.WebElement (session="f68a9f0d3dea9910d1082f547c75d945", element="0.18112714160758459-6")>]

pypi = None
for menu in menus:
    print(menu.text)
    '''
    Python
    PSF
    Docs
    PyPI
    Jobs
    Community
    '''
    if menu.text == 'PyPI':
        pypi = menu
# click() 함수 실행
pypi.click()
# sleep
time.sleep(5)
# driver종료
browser.quit()

 

- 자동 스크린샷 저장

from selenium import webdriver
import time

# Webdriver 객체생성 : chromedriver.exe 를 실행
driver = webdriver.Chrome('./chromedriver.exe')
driver.get('https://www.naver.com/')
driver.save_screenshot('img/naver.png')

time.sleep(3)
driver.close()

 

 

 

 

 

'Python > Python 웹 크롤링' 카테고리의 다른 글

Python - cine21 데이터 크롤링  (0) 2020.07.31
파이썬 OpenAPI_07월 24일  (0) 2020.07.24
파이썬 OpenAPI_07월 23일  (0) 2020.07.23
파이썬 OpenAPI_07월 22일  (0) 2020.07.22
파이썬 OpenAPI_07월 21일  (0) 2020.07.21
    'Python/Python 웹 크롤링' 카테고리의 다른 글
    • Python - cine21 데이터 크롤링
    • 파이썬 OpenAPI_07월 24일
    • 파이썬 OpenAPI_07월 23일
    • 파이썬 OpenAPI_07월 22일
    happyso
    happyso

    티스토리툴바