반응형
[selenium 함수]
함수 | 설명 |
webdriver.Chrome("c:/" ) | chromedriver path 지정 하고 사용 |
implicitly_wait(3) | 웹 페이지 로드를 위해 3초 대기 |
get(url) | url에 접근 |
page_source | 현재 렌더링 된 페이지의 Elements를 모두 가져오기 |
find_element_by_name('...') | 페이지의 단일 element 중 name으로 접근 |
find_element_by_id('HTML_id') | id로 접근 |
find_element_by_xpath('xpath') | xpath로 접근 |
find_element_by_css_selector('...') | css selector로 접근 |
find_element_by_class_name('...') | class 이름으로 접근 |
find_element_by_tag_name('...') | tag name으로 접근 |
[Example]
* 맨 처음 한 번만 통과 가능 직접 접근
* 사용못함
from selenium import webdriver
from bs4 import BeautifulSoup as bs
import time
# chromedriver의 위치 지정
driver = webdriver.Chrome("c:/Users/학습러/Desktop/chromedriver_win32/chromedriver.exe")
login_url = "https://nid.naver.com/nidlogin.login"
driver.get(login_url)
driver.find_element_by_id("id").send_keys("myid") # id 넣기
driver.find_element_by_id("pw").send_keys("password") # password 넣기
# 로그인 클릭
driver.find_element_by_xpath('//*[@id="frmNIDLogin"]/fieldset/input').click()
time.sleep(2)
driver.close()
## 2019/02/20 기준 send_keys 함수 사용하면
## 네이버가 자동화된 소프트 웨어라 판단하고 막아놓음
[Example]
* pyperclip 모듈을 이용한 컨트롤 C 컨트롤 V 개념 우회 접근 2
* 가장 많이 나온 자료를 함수화 했다.
* linux에서 사용하기 까다롭다
import pyperclip
userid = 'my_id'
userpw = 'my_pw'
# 클립보드에 input을 복사한 뒤 해당 내용을 actionChain을 이용해 로그인 폼에 붙여넣기
def copy_input(xpath, input):
pyperclip.copy(input)
driver.find_element_by_xpath(xpath).click()
# ActionChains(driver).key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform() # win linux
ActionChains(driver).key_down(Keys.COMMAND).send_keys('v').key_up(Keys.COMMAND).perform() # mac
time.sleep(random.uniform(0,3))
def naver_log_in(id, pw) :
driver.get(url)
driver.implicitly_wait(10)
time.sleep(random.uniform(1,2))
copy_input('//*[@id="id"]', id)
time.sleep(random.uniform(1,2))
copy_input('//*[@id="pw"]', pw)
time.sleep(random.uniform(1,2))
driver.find_element_by_xpath('//*[@id="frmNIDLogin"]/fieldset/input').click()
time.sleep(3)
naver_log_in(userid, userpw)
[Example]
* javascript 를 이용한 우회 접근 3
*사용된 document.getElementsByName('id')[0].value=\' 는 자바스크립트에서 사용되는 함수인데 파이썬에서 키를 직접적으로 넘겨주는 게 아니라 브라우저 내에서 자바스크립트로 아이디 값을 넘겨주기 때문에 네이버의 자동화된 소프트와 알고리즘을 우회하는 원리이다.
# execute_script 함수 사용 (자바스크립트로 아이디, 패스워드를 넘겨주는 형태)
driver.execute_script("document.getElementsByName('id')[0].value=\'"+id+"\'")
driver.execute_script("document.getElementsByName('pw')[0].value=\'"+pw+"\'")
driver.find_element_by_xpath('//*[@id="frmNIDLogin"]/fieldset/input').click()
파이썬(Python) 웹크롤링 : selenium
[selenium 함수] 함수 설명 webdriver.Chrome("c:/...") chrome driver가 설치된 위치 지정하여 사용 implicitly_wait(3) 암묵적으로 모든 웹 자원 로드를 위해 3초 기다림 get(http://url.com’) url에 접근 page..
cceeddcc.tistory.com
반응형
'Dev Log > Python' 카테고리의 다른 글
[Dev log] 파일형식의 log를 DB log 로 튜닝하자 (0) | 2021.01.20 |
---|---|
[Dev log] selenium page down, scroll down, 스크롤 내리기 (1) | 2021.01.14 |
[Dev log] Python 개행 문자(\n) 삭제 - map, lambda, strip (0) | 2021.01.11 |
[Dev log] Python jupyter notebook에서 kenerl이 안보일때 (0) | 2020.06.05 |
[Dev log] Python Crawling (0) | 2020.04.23 |