BASHA TECH
네이버 매크로 예제 본문
728x90
terminal>
(pandas-dev) D:\big15\pandas-dev>pip install selenium
Collecting selenium
Downloading selenium-4.5.0-py3-none-any.whl (995 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 995.2/995.2 kB 15.9 MB/s eta 0:00:00
Collecting urllib3[socks]~=1.26
Downloading urllib3-1.26.12-py2.py3-none-any.whl (140 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 140.4/140.4 kB 2.1 MB/s eta 0:00:00
Requirement already satisfied: certifi>=2021.10.8 in d:\anaconda3\envs\pandas-dev\lib\site-packages (from selenium) (2022.9.14)
Collecting trio~=0.17
Downloading trio-0.22.0-py3-none-any.whl (384 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 384.9/384.9 kB 12.1 MB/s eta 0:00:00
Collecting trio-websocket~=0.9
Downloading trio_websocket-0.9.2-py3-none-any.whl (16 kB)
Collecting async-generator>=1.9
Downloading async_generator-1.10-py3-none-any.whl (18 kB)
Collecting sniffio
Downloading sniffio-1.3.0-py3-none-any.whl (10 kB)
Collecting sortedcontainers
Downloading sortedcontainers-2.4.0-py2.py3-none-any.whl (29 kB)
Collecting attrs>=19.2.0
Downloading attrs-22.1.0-py2.py3-none-any.whl (58 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.8/58.8 kB 3.0 MB/s eta 0:00:00
Collecting exceptiongroup>=1.0.0rc9
Downloading exceptiongroup-1.0.0rc9-py3-none-any.whl (12 kB)
Collecting idna
Downloading idna-3.4-py3-none-any.whl (61 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.5/61.5 kB 1.7 MB/s eta 0:00:00
Collecting outcome
Downloading outcome-1.2.0-py2.py3-none-any.whl (9.7 kB)
Collecting cffi>=1.14
Downloading cffi-1.15.1-cp38-cp38-win_amd64.whl (178 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 178.8/178.8 kB 5.3 MB/s eta 0:00:00
Collecting wsproto>=0.14
Downloading wsproto-1.2.0-py3-none-any.whl (24 kB)
Collecting PySocks!=1.5.7,<2.0,>=1.5.6
Downloading PySocks-1.7.1-py3-none-any.whl (16 kB)
Collecting pycparser
Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 118.7/118.7 kB 3.5 MB/s eta 0:00:00
Collecting h11<1,>=0.9.0
Downloading h11-0.14.0-py3-none-any.whl (58 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.3/58.3 kB 1.0 MB/s eta 0:00:00
Installing collected packages: sortedcontainers, urllib3, sniffio, PySocks, pycparser, idna, h11, exceptiongroup, attrs, async-generator, wsproto, outcome, cffi, trio, trio-websocket, selenium
Successfully installed PySocks-1.7.1 async-generator-1.10 attrs-22.1.0 cffi-1.15.1 exceptiongroup-1.0.0rc9 h11-0.14.0 idna-3.4 outcome-1.2.0 pycparser-2.21 selenium-4.5.0 sniffio-1.3.0 sortedcontainers-2.4.0 trio-0.22.0 trio-websocket-0.9.2 urllib3-1.26.12 wsproto-1.2.0
from selenium import webdriver
from selenium.webdriver.common.by import By
chrome > 도움말 > chrome 정보 => 브라우저 버전 확인
chromedriver에서 해당 버전에 맞는 것 다운로드 하기
https://chromedriver.chromium.org/downloads
# driver memory load
driver = webdriver.Chrome('../driver/chromedriver.exe')
코드로 제어해서 네이버를 연다.
driver.get('https://www.naver.com')
# id 입력
# id 입력창 선택
elem_id = driver.find_element(By.ID,'id')
# 기존 값을 삭제
elem_id.clear() # 앞에 타이핑 한 것이 지워짐
# 값 설정
elem_id.send_keys('gogogo')
-----------------------
# 비밀번호 처리
# 비밀번호 선택
elem_pw = driver.find_element(By.ID, 'pw')
# 기존 값 삭제
elem_pw.clear()
# 값 설정
elem_pw.send_keys('1234567') #key를 보내라
-----------------------
# 로그인 버튼 클릭
# 로그인 버튼 선택
# //*[@id="log.login"] : xpath => 줄여진 단축 경로
elem_login = driver.find_element(By.XPATH, '//*[@id="log.login"]')
# 클릭
elem_login.click()
# email.naver.com
driver.get('https://mail.naver.com')
from bs4 import BeautifulSoup
html = driver.page_source
html
html 전체 긁어옴...
soup = BeautifulSoup(html, 'html.parser')
raw_list = soup.find_all('div', class_='name _ccr(lst.from)')
raw_list
Output exceeds the size limit. Open the full output data in a text editor
[<div class="name _ccr(lst.from)"><span class="blind">보낸 이:</span><a class="_c1(myContextMenu|showSenderContextLayer|list|1515) _stopDefault" href="#" title='"네이버" <account_noreply@navercorp.com>'>네이버</a></div>, <div class="name _ccr(lst.from)"><span class="blind">보낸 이:</span><a class="_c1(myContextMenu|showSenderContextLayer|list|1513) _stopDefault" href="#" title='"네이버 블로그" <blogpeople@navercorp.com>'>네이버 블로그</a></div>, <div class="name _ccr(lst.from)"><span class="blind">보낸 이:</span><a...
# 메일 제목 추출
send_list = [raw_list[n].find('a').get_text() for n in range(0,len(raw_list))]
send_list
Output exceeds the size limit. Open the full output data in a text editor
['네이버', '네이버 블로그', '네이버', '해피빈', '네이버', '네이버', '네이버', ...
driver.close()
브라우저 닫음
728x90
반응형
'Computer > Pandas' 카테고리의 다른 글
Predict survival on the Titanic 타이타닉 생존자 예측 예제 1 (1) | 2022.10.04 |
---|---|
셀프 주유소 가격 분석 (1) | 2022.10.04 |
시카고 맛집 분석 예제 (1) | 2022.09.30 |
Ch12. 시계열 데이터 (0) | 2022.09.29 |
Ch11. 그룹 연산 (0) | 2022.09.29 |
Comments