๐ŸŒฑ → ๐ŸŒณ

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

Server/Python

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

BAY 2022. 4. 8. 16:24
728x90

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

๐Ÿ“Œํฌ๋กค๋ง ๋Œ€์ƒ ์‚ฌ์ดํŠธ ์‚ดํŽด๋ณด๊ธฐ

์˜ค๋Š˜์€ ํŒŒํŒŒ๊ณ ๋ฅผ ์‚ฌ์šฉํ•œ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด ๋ด„

 

๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด ํŒŒํŒŒ๊ณ ์— ์ ‘์†ํ•˜์—ฌ ์ง์ ‘ ์‚ฌ์ดํŠธ๋ฅผ ์‚ดํŽด๋ด„

https://papago.naver.com/

 

๋„ค์ด๋ฒ„ ํŒŒํŒŒ๊ณ 

๋ฒˆ์—ญ์„ ๋ถ€ํƒํ•ด ํŒŒํŒŒ๊ณ 

papago.naver.com

 

1๏ธโƒฃ ๋™์ ํฌ๋กค๋ง ์ง„ํ–‰ ์ˆœ์„œ

๋™์ ํฌ๋กค๋ง์˜ ๊ฒฝ์šฐ, ๋ฐ์ดํ„ฐ๋ฅผ ์ฆ‰์‹œ ์ˆ˜์ง‘ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— 

๋ฐ์ดํ„ฐ๋ฅผ ์–ป๊ธฐ ์œ„ํ•œ ์ˆœ์„œ๋ฅผ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”

 

ํŒŒํŒŒ๊ณ ์— ์ ‘์†ํ•ด์„œ ๋ฒˆ์—ญ์„ ๋ฐ›๊ธฐ ์œ„ํ•ด, ์•„๋ž˜ ์ž‘์—…์„ ๊ฑฐ์นจ

  1. ๋ฒˆ์—ญํ•  ๋‚ด์šฉ์„ ์™ผ์ชฝ ์ฐฝ์— ์ž…๋ ฅ
  2. ๋ฒˆ์—ญ ๋ฒ„ํŠผ์„ ํด๋ฆญ
  3. ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๋ฅผ ์ˆ˜์ง‘

(ํŒŒํŒŒ๊ณ ๋Š” ๋ฒˆ์—ญ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด์ง€ ์•Š์•„๋„ ์ž๋™์œผ๋กœ ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค์ง€๋งŒ

๋ˆ„๋ฝ ๋  ๊ฒฝ์šฐ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ฒˆ์—ญ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋Š” ์ž‘์—…์„ ํฌํ•จ)

 

2๏ธโƒฃ ํ•„์š”ํ•œ ์š”์†Œ์˜ ํƒœ๊ทธ ๋ฐ ์„ ํƒ์ž ํ™•์ธ

ํŒŒํŒŒ๊ณ ์˜ html ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๊ณ  3๊ฐœ ์š”์†Œ์˜ ํƒœ๊ทธ ๋ฐ ์„ ํƒ์ž ํ™•์ธ

โœ” ๋ฒˆ์—ญ ์ž…๋ ฅ

 

# ๋ฒˆ์—ญ ์ž…๋ ฅ ์นธ
"textarea#txtSource"

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฒˆ์—ญ ๋ฒ„ํŠผ๊ณผ ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๋„ ์ฐพ์•„์คŒ

# ๋ฒˆ์—ญ ๋ฒ„ํŠผ
"button#btnTranslate"
# ๋ฒˆ์—ญ ๊ฒฐ๊ณผ
"div#tetTarget.edit_box___1KtZ3.active___3VPGL"

 

๐Ÿ“ŒํŒŒ์ด์ฌ ์ฝ”๋“œ ์ž‘์„ฑ

1๏ธโƒฃ ์‚ฌ์ „์ž‘์—…

from selenium import webdriver 
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time

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

2๏ธโƒฃ ํŒŒํŒŒ๊ณ  ํŽ˜์ด์ง€๋กœ ์ด๋™

from selenium import webdriver 
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time

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

URL = "https://papago.naver.com/"
driver.get(URL)
time.sleep(3)

3๏ธโƒฃ ์˜๋‹จ์–ด ์ž…๋ ฅ๋ฐ›๊ธฐ

inputํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ฒˆ์—ญํ•˜๊ณ  ์‹ถ์€ ์˜๋‹จ์–ด ์ž…๋ ฅ ๋ฐ›์Œ

question = input("๋ฒˆ์—ญ ํ•  ์˜๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”. : ")

4๏ธโƒฃ ํƒœ๊ทธ ์„ ํƒ, find_element ํ•จ์ˆ˜์™€ By

 

  1. ์ž…๋ ฅ๊ฐ’์„ ๋‹ด๋‹นํ•˜๋Š” HTML ํƒœ๊ทธ ์„ ํƒ
  2. ๋ฐ์ดํ„ฐ ์ „์†ก

find_element

from selenium.webdriver.common.by import By

driver.find_element(By.๊ฒ€์ƒ‰๋ฐฉ๋ฒ•, "๊ฒ€์ƒ‰์–ด" )

find_element ํ•จ์ˆ˜๋Š” ๊ฒ€์ƒ‰ ๋ฐฉ๋ฒ•๊ณผ ๊ฒ€์ƒ‰์–ด, ๋‘ ๊ฐ€์ง€ ์ธ์ž๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์Œ

 

๊ฒ€์ƒ‰ ๋ฐฉ๋ฒ•์€ xpath, css_selector, id ๋“ฑ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์กด์žฌ 

์˜ค๋Š˜์€ CSS_Selector ์‚ฌ์šฉ!

 

driver.find_element(By.CSS_SELECTOR, " ํƒœ๊ทธ์™€ ์„ ํƒ์ž์กฐํ•ฉ ")

๊ฒ€์ƒ‰์–ด๋กœ ์›ํ•˜๋Š” HTML ์š”์†Œ์˜ ํƒœ๊ทธ์™€ ์„ ํƒ์ž๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๊ฒ€์ƒ‰๋œ HTML ์š”์†Œ๋ฅผ ๋ฆฌํ„ดํ•ด ์คŒ 

form = driver.find_element(By.CSS_SELECTOR, "textarea#txtSource")

 

5๏ธโƒฃ ๋ฐ์ดํ„ฐ ์ „์†ก, send_keys

 

send_key

: HTML ์š”์†Œ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๋Š” ํ•จ์ˆ˜ 

"Selenium์˜ HTML์š”์†Œ".send_keys("์ „์†กํ•  ๊ฐ’")
form = driver.find_element(By.CSS_SELECTOR, "textarea#txtSource")
form.send_keys(question)

 

6๏ธโƒฃ ๋ฒˆ์—ญ ๋ฒ„ํŠผ ํด๋ฆญ

click : HTML ์š”์†Œ๋ฅผ ํด๋ฆญํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ 

button = driver.find_element(By.CSS_SELECTOR, "button#btnTranslate")
button.click()
time.sleep(2)

๋ฒˆ์—ญํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ์„ ํƒํ•˜๊ณ  ํด๋ฆญ

 

7๏ธโƒฃ ๋ฒˆ์—ญ ๊ฒฐ๊ณผ ์ถœ๋ ฅ

result = driver.find_element(By.CSS_SELECTOR, "div#txtTarget")
print(question, "->", result.text)

๋ฒˆ์—ญ ๊ฒฐ๊ณผ ๋ฐ›์•„์˜ด

 

8๏ธโƒฃ ํฌ๋กฌ ์ฐฝ ๋‹ซ๊ธฐ

driver.close()

 

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

โ€‹โœ” ์›น ํŽ˜์ด์ง€ ์‚ดํŽด๋ณด๊ธฐ

โœ” driver๋ฅผ ํ™œ์šฉํ•œ ์„ ํƒ์ž ์‚ฌ์šฉ

โœ” ์ž…๋ ฅํ•˜๊ธฐ

- ์ž…๋ ฅ์„ ์œ„ํ•œ ํ•จ์ˆ˜ send_keys( )

- ํด๋ฆญ์„ ์œ„ํ•œ ํ•จ์ˆ˜ click( )

โœ” ๊ฒฐ๊ณผ ๊ฐ€์ ธ์˜ค๊ธฐ

728x90