Deepl Translator の翻訳機能は、現在市場で比較的正確な専門の翻訳ツールですが、その API は高価であり、中国本土では購入することができません。現在、GitHub 上には API を使用したいくつかの無料の Deepl 翻訳コードがありますが、ほとんどが機能しなくなり、数回使用すると「too many requests」というエラーが表示されます。
私は多くの改善方法を試しましたが、うまくいきませんでした。この方法は通用しません。
ウェブブラウザで使用されている無料の翻訳は制限がないようですので、なぜ Selenium ヘッドレスブラウザを使用して翻訳機能を実装しないのでしょうか?
言ってみれば、やるしかありません。コードを書き始めます:
#!/usr/bin/python
# -*- coding:utf-8 -*-
'''Seleniumをインストールする必要があります。geckodriverを使用するため、まずFirefoxブラウザをインストールする必要があります。'''
import re
import hashlib
import urllib
from urllib.parse import unquote
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
from selenium.webdriver.support.wait import WebDriverWait
import traceback
import time
import webDeeplTran
from Log import Log
logger = Log(__name__).getlog()
def getDeeplLink(check_url):
driver = None
domain = None
try:
options = webdriver.FirefoxOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--headless')
options.add_argument('--window-size=3456,2160')
driver = webdriver.Firefox(options=options)
driver.get(check_url)
# ページの読み込みが完了するまで待機
driver.implicitly_wait(60)
# テキストエリア要素を検索してテキストを入力
input_text_area = driver.find_element(By.XPATH, '/html/body/div[4]/main/div[5]/div[1]/div[2]/section[1]/div[3]/div[2]/d-textarea/div')
input_text_area.send_keys(' The command Get Computed Label returns the accessibility label (sometimes\nalso referred to as Accessible Name), which is a short string that labels the\nfunction of the control (e.g. the string "Comment" or "Sign In" on a button).\n\nThe command Get Computed Role returns the reserved token value (in ARIA,\nbutton, heading, etc.) that describes the type of control or content in the\nelement.')
time.sleep(10)
output_text_area = driver.find_element(By.XPATH, '/html/body/div[4]/main/div[5]/div[1]/div[2]/section[2]/div[3]/div[1]/d-textarea/div')
output_text = output_text_area.get_attribute('textContent')
logger.debug(output_text)
# input_text_area.clear()
# input_text_area.send_keys(" Selenium recently removed the 16 deprecated find_element(s)_by_x functions in favor of a general find_element and find_elements function that take the 'by' part as their first argument.\nTo update your code, you can use your IDE's find-and-replace-all feature to replace these 16 search terms:")
# time.sleep(10)
# output_text = output_text_area.get_attribute('textContent')
# output_html = output_text_area.get_property('innerHTML')
# output_text = BeautifulSoup(output_html, 'html.parser').get_text()
# logger.debug(output_text)
# JavaScriptコードを実行してdiv要素のlang属性を設定
# driver.execute_script("document.getElementById('target-dummydiv').setAttribute('lang', 'zh-CN');")
# スクリーンショットを撮影してD:\\ディレクトリに保存
# driver.save_screenshot('D:\\deepl.png')
# ブラウザを閉じる
driver.quit()
except Exception as e:
logger.debug(traceback.format_exc())
finally:
if driver:
driver.quit()
if __name__ == "__main__":
getDeeplLink('https://www.deepl.com/translator')
上記のコードを実行すると、まずスクリーンショット機能を使用してヘッドレスブラウザが正常に表示されるかどうかを確認します:
driver.save_screenshot('D:\deepl.png')
ほぼ完璧に表示されます。
いくつかの重要なポイント:
- これは入力ボックスを取得し、翻訳する英文のテキストを入力するためのものです。
input_text_area = driver.find_element(By.XPATH, '/html/body/div[4]/main/div[5]/div[1]/div[2]/section[1]/div[3]/div[2]/d-textarea/div')
input_text_area.send_keys メソッドを使用して入力します。
-
翻訳後の中国語のテキストボックスオブジェクトを取得します。
output_text_area = driver.find_element(By.XPATH, '/html/body/div[4]/main/div[5]/div[1]/div[2]/section[2]/div[3]/div[1]/d-textarea/div')
翻訳後のテキストを取得します。
output_text = output_text_area.get_attribute('textContent') -
英文のテキストを入力した後、翻訳後のテキストを読み込むために N 秒待つ必要があるため、ここでは time.sleep (10) を使用しています。
-
繰り返し翻訳文を入力する場合、以前のテキストをクリアする必要があります。以下の方法を使用します:
input_text_area.clear() -
私は Firefox ブラウザと geckodriver.exe ドライバを使用していますが、最新バージョンをインストールするだけで問題ありません。Chrome を試しましたが成功しませんでした。インストールに問題があるのかもしれませんが、成功した場合は経験を共有してください。
-
私は英語から中国語への翻訳のみを実装しましたが、他の言語の翻訳については研究していません。成功した場合は、共有していただければ幸いです。