Python E化-自制有道英文翻译器
import requests
import json
from tkinter import Tk,Button,Entry,Label,Text,END
class YouDaoFanyi(object):
def __init__(self):
pass
def crawl(self,word):
url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data={'i': word,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTIME',
'typoResult': 'false'}
r = requests.post(url, data)
answer=json.loads(r.text)
result_list=[]
for i in range(len(answer['translateResult'][0])):
result_sub = answer['translateResult'][0][i]['tgt']
result_list.append(result_sub)
result=''.join(result_list)
return result
class JinshanFanyi(object):
def __init__(self):
pass
def translate_iciba(self,myword):
url = 'http://fy.iciba.com/ajax.php?a=fy'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'}
data = {
'f': 'auto',
't': 'auto',
'w': myword,}
translate_request = requests.post(url,headers=headers,data=data)
js_translate=translate_request.json()
mytranslator_iciba=js_translate['content']['out']
print('金山翻译:'+mytranslator_iciba)
return mytranslator_iciba
class Application(object):
def __init__(self):
self.window = Tk()
self.fanyi = YouDaoFanyi()
self.fanyi_jinshan=JinshanFanyi()
self.window.title(u'Season翻译器')
self.window.geometry('620x580+500+10')
self.window.minsize(620,580)
self.window.maxsize(620,580)
self.title_label = Label(self.window,text=u'翻译原文:')
self.title_label.place(x=10,y=10)
self.result_text1 = Text(self.window,background = 'azure')
self.result_text1.place(x = 10,y = 30,width = 580,height = 150)
self.submit_btn = Button(self.window,text=u'翻译',command=self.submit)
self.submit_btn.place(x=505,y=185,width=35,height=25)
self.submit_btn2 = Button(self.window,text=u'清空',command = self.clean)
self.submit_btn2.place(x=550,y=185,width=35,height=25)
self.title_label = Label(self.window,text=u'有道翻译结果:')
self.title_label.place(x=10,y=200)
self.title_label_jinshan = Label(self.window,text=u'金山翻译结果:')
self.title_label_jinshan.place(x=10,y=380)
self.result_text = Text(self.window,background = 'light cyan',font=('Arial',11))
self.result_text.place(x = 10,y = 220,width = 580,height = 150)
self.result_text_jinshan = Text(self.window,background = 'light cyan',font=('Arial',11))
self.result_text_jinshan.place(x = 10,y = 400,width = 580,height =150)
def submit(self):
content = self.result_text1.get(0.0,END).strip().replace("\n"," ")
result = self.fanyi.crawl(content).strip()
self.result_text.delete(0.0,END)
self.result_text.insert(END,result)
result_jinshan = self.fanyi_jinshan.translate_iciba(content)
self.result_text_jinshan.delete(0.0,END)
self.result_text_jinshan.insert(END,result_jinshan)
def clean(self):
self.result_text1.delete(0.0,END)
self.result_text.delete(0.0,END)
self.result_text_jinshan.delete(0.0,END)
def run(self):
self.window.mainloop()
if __name__=="__main__":
app = Application()
app.run()