翻译
2020-10-14 本文已影响0人
月夜星空下
from tkinter import *
from tkinter import messagebox
import requests
'''搭建页面'''
# 1 获取用户输入的词汇
# 2 爬取有道翻译结果 反爬。
# 3 根据获取到的结果显示到窗口上
'''完成翻译'''
# content = entry.get()
while 1<2:
content = input("请输入内容:")
content = content.strip()#去除空格。split():分割
if content == '':
# messagebox.showinfo("提示",message='请输入要翻译的内容')
pass
else:
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=true'
data ={}
data['i'] = content
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CLICKBUTTON'
data['typeResult'] = 'false'
resu = requests.post(url,data=data)
result = resu.json()
tran = result['translateResult'][0][0]['tgt']
tran = tran.strip() # 去除空格。split():分割
if tran == '':
# messagebox.showinfo("提示",message='请输入要翻译的内容')
pass
else:
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=true'
data = {}
data['i'] = tran
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CLICKBUTTON'
data['typeResult'] = 'false'
resu = requests.post(url, data=data)
result = resu.json()
trans = result['translateResult'][0][0]['tgt']
print(trans)