今天为大家介绍一款有道翻译的软件
废话不多说,先上代码
环境:python3
import requests
from tkinter import Tk,Button,Entry,Label,Text,END
class youdaohelper(object):
def __init__(self):
pass
def crawl(self,content):
data = {
'i': content,
'from': 'AUTO',
'to': 'AUTO',
# 'smartresult':'dict',
'client': 'fanyideskweb',
# 'salt':'15845869423826',
# 'sign':'9046c8954cf2d2b516552f51f65f47ed',
'ts': '1584586942382',
'bv': 'cc652a2ad669c22da983a705e3bca726',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_CLICKBUTTION'
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
result = requests.post(url, data, headers=headers)
p = result.json()['translateResult'][0][0]['tgt']
return p
class Application(object):
def __init__(self):
self.helper = youdaohelper()
self.window = Tk()
self.window.title(u'有我在手 英语无忧')
self.window.geometry("280x350+1050+10")
self.window.attributes('-topmost',True)
# 输入框
self.entry = Entry(self.window)
self.entry.place(x=10, y=10, width=200, height=25)
# 提交按钮
self.submit_btn = Button(self.window, text=u'查询', command=self.submit)
self.submit_btn.place(x=220, y=10, width=50, height=25)
# 翻译结果标题
self.title_label = Label(self.window, text=u'翻译结果')
self.title_label.place(x=10, y=55, height=25)
# 翻译结果
self.result_text = Text(self.window, background='white')
self.result_text.place(x=10, y=75, width=260, height=265)
def submit(self):
content=self.entry.get()
p=self.helper.crawl(content)
self.result_text.delete(1.0,END)
self.result_text.insert(END,p)
def run(self):
self.window.mainloop()
if __name__=='__main__':
app=Application()
app.run()
运行结果如图所示
(https://img-blog.csdnimg.cn/20200327221650590.png)
[这是一直置顶的]