-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslateUtil.py
61 lines (55 loc) · 2.24 KB
/
translateUtil.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from translate import Translator
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options
def translate(sentence):
translator = Translator(from_lang="ZH", to_lang="EN-US")
translation = translator.translate(sentence)
return translation
def openBaiduFanYi(url):
# 无可视化界面设置 #
edge_options = Options()
# 使用无头模式
edge_options.add_argument('--headless')
# 禁用GPU,防止无头模式出现莫名的BUG
edge_options.add_argument('--disable-gpu')
# 指定msedgedriver位置和无头模式的配置
# browser = webdriver.Edge("C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe", options=edge_options)
# 使用打开浏览器的方式
browser = webdriver.Edge(r"D:\four2\Graduating Design\nlp\msedgedriver.exe", options=edge_options)
# 打开百度翻译网页
browser.get(url)
return browser
def translate_baidu(sentence):
browser = openBaiduFanYi("https://fanyi.baidu.com")
tryCount = 5
res = ""
while tryCount > 0:
try:
input_baidu = browser.find_elements(By.ID, "baidu_translate_input")
input_baidu[0].clear()
input_baidu[0].send_keys(sentence)
time.sleep(2) # 等待2S后取回翻译结果
res = browser.find_elements(By.XPATH, "//*[@class='ordinary-output target-output clearfix']")[0].text
tryCount = 0
except Exception as e:
tryCount -= 1
return res
def translate_youdao(sentence):
browser = openBaiduFanYi("https://fanyi.youdao.com/index.html#/")
tryCount = 5
res = ""
while tryCount > 0:
try:
input_youdao = browser.find_elements(By.ID, "js_fanyi_input")
input_youdao[0].clear()
input_youdao[0].send_keys(sentence)
time.sleep(2) # 等待2S后取回翻译结果
res = browser.find_elements(By.XPATH, "//*[@id=\"js_fanyi_output_resultOutput\"]/p/span")[0].text
tryCount = 0
except Exception as e:
tryCount -= 1
return res
if __name__=="__main__":
print(translate_baidu("好人一生平安"))