๐ŸŒฑ → ๐ŸŒณ

๋ชจ๊ฐ์ฝ” ํŒŒ์ด์ฌ ํฌ๋กค๋ง 15์ผ์ฐจ ๋ณธ๋ฌธ

Server/Python

๋ชจ๊ฐ์ฝ” ํŒŒ์ด์ฌ ํฌ๋กค๋ง 15์ผ์ฐจ

BAY 2022. 4. 8. 17:10
728x90

#15. ๋™์  ํฌ๋กค๋ง โ‘ค

๐Ÿ“Œ ์‹ ๊ทœ ํšŒ์› ๊ฒŒ์‹œํŒ

XPath ์‚ฌ์šฉ ์ด์œ  

1. id, class์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š๊ณ  ํฌ๋กค๋ง์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. 

2, html ์š”์†Œ๋“ค์˜ ํŒจํ„ด์„ ํŒŒ์•…ํ•˜๊ธฐ ์‰ฝ๋‹ค

 

โœ… XPath์˜ ํŒจํ„ด

์ตœ์ƒ๋‹จ ๊ธ€์˜ XPath

/html/body/div[1]/div/div[4]/table/tbody/tr[1]/td[1]/div[3]/div/a

๊ทธ ๋ฐ”๋กœ ์•„๋ž˜ ๊ฒŒ์‹œ๊ธ€์˜ XPath

/html/body/div[1]/div/div[4]/table/tbody/tr[2]/td[1]/div[3]/div/a

์„ธ๋ฒˆ์งธ ๊ฒŒ์‹œ๊ธ€์˜ XPath

/html/body/div[1]/div/div[4]/table/tbody/tr[3]/td[1]/div[3]/div/a

 

์ค‘๊ฐ„์— ์กด์žฌํ•˜๋Š” tr ํƒœ๊ทธ์˜ ๋ฒˆํ˜ธ๊ฐ€ ํ•˜๋‚˜์”ฉ ์ฆ๊ฐ€ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธ

-> ์ด์ฒ˜๋Ÿผ ํŒจํ„ด์„ ํŒŒ์•…ํ•˜๊ธฐ๊ฐ€ ๊ต‰์žฅํžˆ ์‰ฌ์›€

 

ํ•œ ํŽ˜์ด์ง€์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜ ๋ณด์•˜๋‹ค. 

 

โœ… ํŒŒ์ด์ฌ ์ฝ”๋“œ ๊ตฌํ˜„

์˜ค๋Š˜์€ ๊ฒŒ์‹œ๊ธ€์˜ XPath ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด,

์œ„ ๊ณผ์ • ์‚ฌ์ด์— ๋’ค๋กœ๊ฐ€๊ธฐ๋ฅผ ์‚ฝ์ž…ํ•ด ์ค„๊ฒƒ์ด๋‹ค. 

 

๊ฒฐ๊ณผ์ ์œผ๋กœ ๋กœ๊ทธ์ธ - ๊ฒŒ์‹œํŒ ์ด๋™ - ๊ฒŒ์‹œ๊ธ€ ์ง„์ž… - ๋‚ด์šฉ์ถ”์ถœ - ๋’ค๋กœ๊ฐ€๊ธฐ - ๊ฒŒ์‹œ๊ธ€ ์ง„์ž… - ...

์ˆœ์„œ๋กœ ๋ฐ˜๋ณต๋œ๋‹ค. 

 

2๏ธโƒฃ ๋’ค๋กœ ๊ฐ€๊ธฐ

driver.back()

 

3๏ธโƒฃ XPath ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด ๋ฐ˜๋ณต๋ฌธ ๊ตฌํ˜„

 

XPath ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด 

15๋ฒˆ์งธ ๊ฒŒ์‹œ๊ธ€๊นŒ์ง€ ๋ชจ๋“  ๋‚ด์šฉ์„ ๋ฐ›์•„์™€ ๋ณด์ž 

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time

chrome_driver = ChromeDriverManager().install()
service = Service(chrome_driver)
driver = webdriver.Chrome(service=service)

login_url = "https://nid.naver.com/nidlogin.login"
driver.get(login_url)

time.sleep(2)

my_id = "ID"
my_pw = "PW"

driver.execute_script("document.getElementsByName('id')[0].value = '" + my_id + "'")
driver.execute_script("document.getElementsByName('pw')[0].value = '" + my_pw + "'")

time.sleep(1)

button = driver.find_element(By.ID, "log.login")
button.click()
time.sleep(1)

comu_url = "https://cafe.naver.com/codeuniv"
driver.get(comu_url)
time.sleep(1)

menu = driver.find_element(By.ID, "menuLink90")
menu.click()
time.sleep(1)

for i in range(1, 16):
    xpath = (
        "/html/body/div[1]/div/div[4]/table/tbody/tr[" + str(i) + "]/td[1]/div[3]/div/a"
    )

    driver.switch_to.frame("cafe_main")
    time.sleep(1)

    writing = driver.find_element(By.XPATH, xpath)
    writing.click()
    time.sleep(1)

    content = driver.find_element(By.CSS_SELECTOR, "div.se-component-content").text
    print(content)

    driver.back()
    time.sleep(1)

driver.close()

 

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ 

๋„ค์ด๋ฒ„ ์นดํŽ˜๋Š” ํ”„๋ ˆ์ž„์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์–ด 

๊ธฐ๋ณธ ํฌ๋กค๋ง ๋ฐฉ์‹์œผ๋กœ๋Š” ๊ฒ€์ƒ‰์ด ๋˜์ง€ ์•Š๋Š”๋‹ค 

๋”ฐ๋ผ์„œ ๋ฐ˜๋ณต๋ฌธ ๋‚ด๋ถ€์— swith_to.frame ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด, ๊ฒŒ์‹œํŒ์œผ๋กœ ๋Œ์•„๊ฐˆ ๋•Œ๋งˆ๋‹ค

์…€๋ ˆ๋‹ˆ์›€์˜ ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ cafe_main์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

โญ์ •๋ฆฌํ•˜๊ธฐโญ

โœ” ๋’ค๋กœ๊ฐ€๊ธฐ

โœ” XPath์˜ ํŒจํ„ด ํŒŒ์•…

 

728x90