본문 바로가기

Python_Crawling/Crawling

[Naver]네이버 메일 제목 가져오기 - 클립보드 사용

반응형

https://developer-ankiwoong.tistory.com/55

 

Python - NaverMail 제목 리스트 가져오기

< 실행 코드 > from selenium import webdriver driver = webdriver.Chrome('C:/chromedriver/chromedriver') driver.implicitly_wait(3) driver.get('https://nid.naver.com/nidlogin.login?mode=number') # 일회..

developer-ankiwoong.tistory.com

맨 처음 했던 방식이 저 위에 방식이라면 이번에는 방식을 변경해서 그림문자를 안받고 하는 방법을 해본다.

 

임시비밀번호를 사용해 네이버 제목 리스트를 가져왔다면 이번에는 다른 방법으로 사용해보자.

 

모듈은 clipboard 을 사용한다.

 

1. clipboard 모듈 설치

pip install clipboard

 

2. import 모듈

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import clipboard
import time

 

3. id / pw 를 변수로 지정

id = ''
pw = ''

 

4. WebDriver 로드

driver = webdriver.Chrome(executable_path='C:\\chromedriver\\chromedriver')
driver.implicitly_wait(3)

 

5. 사이트 로드

driver.get(
    'https://nid.naver.com/nidlogin.login?mode=form&url=https%3A%2F%2Fwww.naver.com')

 

6. id를 클립보드에 복사

clipboard.copy(id)

 

7. ID 부분 xpath 추출 후 클릭 적용

 

driver.find_element_by_xpath('//*[@id="id"]').click()

 

8. ActionChains 사용

https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains

 

7. WebDriver API — Selenium Python Bindings 2 documentation

A single IP address, as a string. If any IPv4 address is found, one is returned. Otherwise, if any IPv6 address is found, one is returned. If neither, then None is returned.

selenium-python.readthedocs.io

 

ActionChains(driver).key_down(Keys.CONTROL).send_keys(
    'v').key_up(Keys.CONTROL).perform()

 

* 클립보드에 보낸 id 값을 ctrl + v로 붙여 넣는다.

 

9. pw를 클립보드에 복사

clipboard.copy(pw)

 

10. PW 부분 xpath 추출 후 클릭 적용

 

driver.find_element_by_xpath('//*[@id="pw"]').click()

 

11. ActionChains 사용

ActionChains(driver).key_down(Keys.CONTROL).send_keys(
    'v').key_up(Keys.CONTROL).perform()

 

12. 로그인 부분 xpath 추출 후 클릭 적용

 

driver.find_element_by_xpath('//*[@id="log.login"]').click()

 

13. 로그인 후 네이버 메일로 이동

driver.get('https://mail.naver.com/')

 

14. 메일 제목 타이틀 추출

titles = driver.find_elements_by_css_selector("strong.mail_title")

 

15. 타이틀 추출 후 리스트 화

for title in titles:
    print(title.text)

 

16. 결과물

 

17. Code

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import clipboard
import time

id = ''
pw = ''

driver = webdriver.Chrome(executable_path='C:\\chromedriver\\chromedriver')
driver.implicitly_wait(3)

driver.get(
    'https://nid.naver.com/nidlogin.login?mode=form&url=https%3A%2F%2Fwww.naver.com')

clipboard.copy(id)
driver.find_element_by_xpath('//*[@id="id"]').click()
ActionChains(driver).key_down(Keys.CONTROL).send_keys(
    'v').key_up(Keys.CONTROL).perform()
time.sleep(1)

clipboard.copy(pw)
driver.find_element_by_xpath('//*[@id="pw"]').click()
ActionChains(driver).key_down(Keys.CONTROL).send_keys(
    'v').key_up(Keys.CONTROL).perform()
time.sleep(1)

driver.find_element_by_xpath('//*[@id="log.login"]').click()
time.sleep(1)

driver.get('https://mail.naver.com/')

titles = driver.find_elements_by_css_selector("strong.mail_title")

for title in titles:
    print(title.text)

driver.quit()

 

반응형