반응형
Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
Tags
- spring boot
- grafana
- jmeter
- spring cloud
- Hibernate
- vue.js
- coalesce
- MySQL
- KAFKA
- cassandra
- spring boot redis
- Redis Sentinel
- mybatis
- RabbitMQ
- WebLogic
- JPA
- netflix oss
- ChannelPipeline
- docker-compose
- QueryDSL
- Airflow
- argo cd
- weblogic 10
- Docker Compose
- redis
- Selenium
- docker compose mysql
- Spring Open Feign
- docker
- Netty
Archives
- Today
- Total
IT.FARMER
selenium 웹크롤링 본문
반응형
selenium 웹크롤링
1. selenium 을 활용한 웹크롤링 방법 예제
def login(userId, user_password):
"""
페이지 로그인
:param userId:
:param user_password:
:return:
"""
oauth_login_url = 'https://nid.naver.com/nidlogin.login?mode=form&url=https%3A%2F%2Fwww.naver.com'
# 페이지 접속
_driver.get(oauth_login_url)
# Element Find
userIdElem = _driver.find_element_by_id("id")
userPasswordElem = _driver.find_element_by_id("pw")
# Value Setting
userIdElem.send_keys(userId)
userPasswordElem.send_keys(user_password)
# page submit
form = _driver.find_element_by_id('frmNIDLogin')
form.submit()
2. webdriver 자주사용하는 메소드
2.1. web element의 id 값으로 찾기
# id_key webelement를 얻어온다.
find_element_by_id("id_key")
2.2 input box 의 value 값 가져오기
# input box 의 value 값을 가져온다.
find_element_by_id("id_input_box_key").get_attribute("value")
find_element_by_css_selector
find_element_by_tag_name
find_element_by_xpath
2.3 select box 선택 후 value값 가져오기
#Selectbox 선택 하기
select = Select(_driver.find_element_by_xpath('x_path_key'))
select.select_by_value('data') # <- data option 선택
2.4 check box, radio button 컨트롤 하기
#checkbox / radio button 은 click event 로 처리 한다.
find_element_by_id("checkbox_key").click()
find_element_by_id("radio_button_key").click()
3. 크롬 드라이버 옵션 설정
def create_chrome_option():
"""
크롬 드라이버 옵션 설정
"""
# 파일 다운로드위치 지정
download_folfer = "C:\\tmp";
options = Options()
options.add_argument("--disable-notifications")
options.add_argument('--verbose')
options.add_experimental_option("prefs", {
"download.default_directory": download_folfer,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing_for_trusted_sources_enabled": False,
"safebrowsing.enabled": False
})
# options.add_argument("start-maximized") # open Browser in maximized mode
options.add_argument("disable-infobars") # disabling infobars
options.add_argument("--disable-extensions") # disabling extensions
options.add_argument("--disable-gpu") # applicable to windows os only
options.add_argument("--no-sandbox") # Bypass OS security model
options.add_argument('--disable-software-rasterizer')
options.add_argument("--disable-extensions") # disabling extensions
options.add_argument("--disable-dev-shm-usage") # overcome limited resource problems
# 브라우저 백그라운드 모드
options.add_argument('--headless')
# 웹드라이버가 실제 웹브라우저인것 처럼 속이기
options.add_argument("--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36")
return options
반응형
'python' 카테고리의 다른 글
ImportError: libSM.so.6: cannot open shared object file: No such file or directory (0) | 2021.07.07 |
---|---|
selenuim 과 requests 비교 (0) | 2019.06.25 |
python datetime 사용방법 (0) | 2019.06.21 |
python 기본 함수 time (0) | 2019.06.21 |
python random 함수 (0) | 2019.06.21 |