๐Ÿ–ฅ๏ธ[Python] 7-2-1. ์›นํฌ๋กค๋ง (selenium ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ)

thisk336ยท2023๋…„ 6์›” 12์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
12/17
post-thumbnail

selenium ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

  • bs4 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋™์  ํŽ˜์ด์ง€๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋™์ ์œผ๋กœ ๋งŒ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ํฌ๋กค๋ง ํ•˜๊ธฐ์œ„ํ•ด์„œ๋Š” selenium ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • Chromedriver ์™€ ๊ฐ™์€ web driver๋ฅผ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž…๋‹ˆ๋‹ค.

  • Chromedriver๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์˜ ๋งํฌ์—์„œ ์ž์‹ ์˜ Chrome ๋ฒ„์ „์— ๋งž๋Š” driver๋ฅผ ๋‹ค์šด๋กœ๋“œ ํ•œ ๋’ค ํ”„๋กœ์ ํŠธ ํด๋”์— ๋„ฃ์–ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    ChromeDriver ๋‹ค์šด๋กœ๋“œ

!pip install selenium
from selenium import webdriver

webdriver.Chrome('chromedriver')
  • selenium์„ ์‚ฌ์šฉํ•  ๋•Œ ํ•ด์•ผ๋˜๋Š” ๊ธฐ๋ณธ์ ์ธ ํ•จ์ˆ˜๊ฐ€ 2๊ฐ€์ง€ ์žˆ์Šต๋‹ˆ๋‹ค.
  • webdriver.Chrome()๊ณผ driver.get() ์ž…๋‹ˆ๋‹ค.
  • webdriver.Chrome()์€ ์‹ค์ œ chromedriver๊ฐ€ ์กด์žฌํ•˜๋Š” ์œ„์น˜์—์„œ chromedriver๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ถˆ๋Ÿฌ์˜ค๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • driver.get()์€ selenium์ด ์ œ์–ดํ•˜๊ณ  ์žˆ๋Š” chromedriver๊ฐ€ ํ•ด๋‹น URL๋กœ ์ ‘์†ํ•˜๊ฒŒ ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# opinet.co.kr์— ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค.

driver = webdriver.Chrome('chromedriver')
driver.get("https://www.opinet.co.kr/searRgSelect.do")
# 2์ดˆ ์‰ฝ๋‹ˆ๋‹ค. (ํŽ˜์ด์ง€ ๋กœ๋”ฉ์„ ์œ„ํ•ด)
time.sleep(2)
# <a href="javascript:goSubPage(0,0,99);"><span>์ง€์—ญ๋ณ„</span></a>
# ๋ฉ”์ธ ํŽ˜์ด์ง€์—์„œ ์œ ๊ฐ€ ๊ฐ€๊ฒฉ ๊ฒ€์ƒ‰ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. javascript๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.
driver.execute_script('goSubPage(0,0,99)')

selenium์—์„œ ์›นํŽ˜์ด์ง€ ์›์†Œ ์ ‘๊ทผํ•˜๊ธฐ

selenium 4.3.0 ๋ฒ„์ „๋ถ€ํ„ฐ๋Š” ๋”์ด์ƒ find_element_by_000 ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•
driver.find_element("xpath", '//*[@id="SIDO_NM0"]')
# ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, '//*[@id="SIDO_NM0"]')
  • find_elements() ํ•จ์ˆ˜์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ต์…˜์„ ์ง€์ •ํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€