Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- ๊ฐ๋ฐ
- KDT
- fe
- ํฌ๋กค๋ง
- ๋๋ฆผ์ฝ๋ฉ
- ๊ทธ๋ฆฌ๋
- ํ์ด์ฌ
- JS
- javascript
- HTML
- Til
- mongodb
- ํ๋ก์ ํธ
- heapq
- ๊ตญ๋น์ง์
- CSS
- ์ฝ๋ฉ์ ํ
- error
- ์๊ณ ๋ฆฌ์ฆ
- ์ฝ๋ฉํ ์คํธ
- react
- ๋ ธ๋ง๋์ฝ๋
- ๋ฐฑ์ค
- ์ฝ๋ฉ
- ํ๋ก๊ทธ๋๋จธ์ค
- ๋ชจ๊ฐ์ฝ
- ํ ์ดํ๋ก์ ํธ
- node.js
- Python
- ํ๋ก ํธ์๋
Archives
- Today
- Total
๐ฑ → ๐ณ
๋ชจ๊ฐ์ฝ ํ์ด์ฌ ํฌ๋กค๋ง 13์ผ์ฐจ ๋ณธ๋ฌธ
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
'Server > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ชจ๊ฐ์ฝ ํ์ด์ฌ ํฌ๋กค๋ง 15์ผ์ฐจ (0) | 2022.04.08 |
---|---|
๋ชจ๊ฐ์ฝ ํ์ด์ฌ ํฌ๋กค๋ง 14์ผ์ฐจ (0) | 2022.04.08 |
๋ชจ๊ฐ์ฝ ํ์ด์ฌ ํฌ๋กค๋ง 12์ผ์ฐจ (0) | 2022.04.08 |
๋ชจ๊ฐ์ฝ ํ์ด์ฌ ํฌ๋กค๋ง 11์ผ์ฐจ (0) | 2022.04.08 |
๋ชจ๊ฐ์ฝ ํ์ด์ฌ ํฌ๋กค๋ง 10์ผ์ฐจ (0) | 2022.04.08 |