๐ŸŒฑ → ๐ŸŒณ

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

Server/Python

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

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

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

๐Ÿ“Œ ๊ตฌํ˜„ํ•  ์†Œ์Šค์ฝ”๋“œ

2๊ฐœ์˜ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•จ

โœ… ์ฒซ ๋ฒˆ์งธ ์†Œ์Šค์ฝ”๋“œ

  • ์˜๋‹จ์–ด ๋ฒˆ์—ญ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰
  • ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ 'my_papago.csv' ํŒŒ์ผ์— ์ €์žฅ

 

โœ… ๋‘ ๋ฒˆ์งธ ์†Œ์Šค์ฝ”๋“œ

  • ์˜๋‹จ์–ด ๋ฒˆ์—ญ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰
  • 'my_papago.csv' ํŒŒ์ผ์— ์žˆ๋Š” ์˜๋‹จ์–ด์ผ ๊ฒฝ์šฐ, ์ €์žฅํ•˜์ง€ ์•Š์Œ
  • 'my_papago.csv' ํŒŒ์ผ์— ์—†๋Š” ์˜๋‹จ์–ด๋Š” ๋ฒˆ์—ญ ๊ฒฐ๊ณผ ์ €์žฅ

 

๐Ÿ“Œ ์ฒซ๋ฒˆ์งธ ํŒŒ์ด์ฌ ์ฝ”๋“œ ์ž‘์„ฑ

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
import csv

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

papago_url = "https://papago.naver.com/"
driver.get(papago_url)

time.sleep(3)

f = open("./my_papago.csv", "w", newline="", encoding="utf-8-sig")

wtr = csv.writer(f)
wtr.writerow(["์˜๋‹จ์–ด", "๋ฒˆ์—ญ๊ฒฐ๊ณผ"])

while True:
    keyword = input("๋ฒˆ์—ญํ•  ์˜๋‹จ์–ด ์ž…๋ ฅ (0 ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ) : ")
    if keyword == "0":
        print("๋ฒˆ์—ญ ์ข…๋ฃŒ")
        break

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

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

    output = driver.find_element(By.CSS_SELECTOR, "div#txtTarget").text

    wtr.writerow([keyword, output])

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

driver.close()

f.close()

 

๐Ÿ‘‰ ์‹คํ–‰ ๊ฒฐ๊ณผ

 

 

๐Ÿ“Œ ๋‘๋ฒˆ์งธ ํŒŒ์ด์ฌ ์ฝ”๋“œ ์ž‘์„ฑ

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
import csv

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

papago_url = "https://papago.naver.com/"
driver.get(papago_url)

time.sleep(3)

f = open("./my_papago.csv", "r", encoding="utf-8-sig")
rdr = csv.reader(f)
next(rdr)

my_dict = {}
for row in rdr:
    keyword = row[0]
    korean = row[1]
    my_dict[keyword] = korean

f.close()

f = open("./my_papago.csv", "a", newline="", encoding="utf-8-sig")
wtr = csv.writer(f)

# while๋ฌธ ์•ˆ์— ์žˆ๋Š” ์กฐ๊ฑด๋ฌธ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”
while True:
    keyword = input("๋ฒˆ์—ญํ•  ์˜๋‹จ์–ด ์ž…๋ ฅ (0 ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ) : ")
    if keyword == "0":
        print("๋ฒˆ์—ญ ์ข…๋ฃŒ")
        break

    # ์˜๋‹จ์–ด๊ฐ€ 'my_dict'์˜ ํ‚ค ๊ฐ’ ์ค‘์— ์žˆ๋‹ค๋ฉด, ์ด ์‚ฌ์‹ค์„ ์•Œ๋ ค์ฃผ๊ณ  ์ €์žฅ๋˜์–ด์žˆ๋˜ ๋ฒˆ์—ญ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
    if keyword in my_dict.keys():
        print("์ด๋ฏธ ๋ฒˆ์—ญํ•œ ์˜๋‹จ์–ด์ž…๋‹ˆ๋‹ค! ๋œป์€", my_dict[keyword], "์ž…๋‹ˆ๋‹ค.")
    # ์œ„์˜ ๊ฒฝ์šฐ์— ํฌํ•จ๋˜์ง€ ์•Š์œผ๋ฉด, ๋”•์…”๋„ˆ๋ฆฌ์™€ CSV ํŒŒ์ผ์— ์ถ”๊ฐ€
    else:
        driver.find_element(By.CSS_SELECTOR, "textarea#txtSource").send_keys(keyword)
        driver.find_element(By.CSS_SELECTOR, "button#btnTranslate").click()
        time.sleep(1)

        output = driver.find_element(By.CSS_SELECTOR, "div#txtTarget").text

        # CSV ํŒŒ์ผ์— ํ–‰ ์ถ”๊ฐ€
        wtr.writerow([keyword, output])

        # ๋”•์…”๋„ˆ๋ฆฌ์— ์ถ”๊ฐ€
        my_dict[keyword] = output

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

driver.close()
f.close()

 

๐Ÿ‘‰ ์‹คํ–‰ ๊ฒฐ๊ณผ

 

 

 

 

 

 

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

โ€‹โœ” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค€๋น„

  • ๋™์ ํฌ๋กค๋ง์„ ์œ„ํ•ด selenium, time
  • csv ํŒŒ์ผ์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด csv

โœ” ์ฒซ๋ฒˆ์งธ ์†Œ์Šค์ฝ”๋“œ

  • csv ํŒŒ์ผ์ƒ์„ฑ
  • writer ๊ฐ์ฒด ์‚ฌ์šฉ
  • csv ํŒŒ์ผ์˜ ์—ด ์ œ๋ชฉ ์ž‘์„ฑ
  • ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฒˆ์—ญ ์—ฌ๋Ÿฌ๋ฒˆ ์‹คํ–‰ ๋ฐ csv ํŒŒ์ผ์— ์ €์žฅ

โœ” ๋‘๋ฒˆ์งธ ์†Œ์Šค์ฝ”๋“œ

  • csv ํŒŒ์ผ์— ์ €์žฅ๋œ ๊ฐ’ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
  • ๋ถˆ๋Ÿฌ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ €์žฅ
  • ํŒŒ์ผ๋‹ซ๊ณ  ๋‹ค์‹œ ์—ด๊ธฐ
  • csv ํŒŒ์ผ์˜ ์—ด ์ œ๋ชฉ ์ž‘์„ฑ
  • ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฒˆ์—ญ ์—ฌ๋Ÿฌ๋ฒˆ ์‹คํ–‰ ๋ฐ csv ํŒŒ์ผ์— ์ถ”๊ฐ€
728x90