이전 포스트에서는 Selenium과 Chrome driver를 활용한 Papago Translation 자동화 에 대해 작성했다.
[Python] Selenium과 Chrome driver를 활용한 Papago Translation 자동화
파파고 translation API는 21년 6월 30일까지 무료 베타 서비스로 제공되었으나 현재는 유료로 바뀌어있다. 약 5천개의 문장을 번역을 해야하는 상황인데, 일회성으로 사용할 것이어서(과금을 하지 않
wookidocs.tistory.com
마찬가지로 Google 번역 API를 사용하고자 하는데 여기도 무료는 아니다.
Google Cloud Translation 에 들어가서 무료 신청을 해서 사용하면 된다.
Cloud Translation | Google Cloud
Cloud Translation을 사용하면 콘텐츠 요구에 따라 선행 학습된 ML 모델 또는 커스텀 ML 모델을 사용해 여러 언어를 동적으로 번역할 수 있습니다.
cloud.google.com
$300 을 무료 크레딧으로 제공하지만, 신청하는 과정이 좀 귀찮다..
그래서 papgo translation과 마찬가지로 selenium과 크롬 드라이버를 이용해서 자동화를 했다.
바로 코드를 공개한다. papago와 html 형식이 크게 다르지는 않지만 frame내부에 id가 없어서 태그를 타고타고 들어가야한다.
<Code>
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
31
32
33
|
import time
from selenium import webdriver
import csv
import pandas as pd
from bs4 import BeautifulSoup as bs
import os
import json
text=['안녕하세요.', '내 이름은 썬샤인이야', '만나서 반가워']
try:
url = "https://translate.google.com/?sl=ko&tl=en&op=translate&text="
# 시크릿모드로 하기 위함
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
browser = webdriver.Chrome(f'chromedriver.exe', chrome_options=chrome_options)
browser.get(url)
browser.implicitly_wait(2)
input_box = browser.find_element_by_css_selector('textarea#txtSource')
button = browser.find_element_by_css_selector('button#btnTranslate')
x_button = browser.find_element_by_class_name('btn_text_clse___1Bp8a')
# text - 리스트에서 하나씩 번역
for i in text:
print(i)
browser.get(url+i)
time.sleep(5)
target_text = browser.find_element_by_xpath('/html/body/c-wiz/div/div[2]/c-wiz/div[2]/c-wiz/div[1]/div[2]/div[2]/c-wiz[2]/div[5]/div/div[1]').text
print(target_text)
time.sleep(3)
print('종료')
except Exception as msg:
error_msg = "번역기 에러:"+str(msg)
browser.close()
|
cs |
papago에서 추가했던 내용을 참고해서 구글 번역기에 언어를 설정하고, text로 바로 파라미터로 넘겨주는 방식을 선택했다.
5초정도 기다려줄 수 있도록 했고, html-body-c-wiz..... 쭉 태그를 따라가서 번역된 텍스트를 가져올 수 있도록 했다.
파파고 자동화 때와 마찬가지로 크롬이 켜지고 혼자 우르르르ㅡㅡㅡ 번역하고 끝난다.
편하다. (복붙해서 하나씩 번역했더라면... 아직도 하고 있을 듯)
'Python' 카테고리의 다른 글
[Python]anaconda를 miniconda로 변경하는 방법(패키지 복사) (0) | 2022.01.19 |
---|---|
[Error] 파이썬 셀레니움 크롬 chrome not reachable 에러 해결 (4) | 2021.12.17 |
[Python] Selenium과 Chrome driver를 활용한 Papago Translation 자동화 (0) | 2021.12.08 |
[Python] 네이버 카페 게시글 크롤러(feat. 크롬 드라이버 & 셀레니움) (6) | 2021.11.23 |
[Python] Flask 및 jQuery를 사용하여 AJAX 파일 업로드 (2) | 2021.11.21 |
댓글