Python实现翻译小工具
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python实现翻译小工具相关的知识,希望对你有一定的参考价值。
一、背景
利用Requests模块获取有道词典web页面的post信息,BeautifulSoup来获取需要的内容,通过tkinter模块生成gui界面。
二、代码
fanyi.py代码如下:
#!/bin/env python
# -*- coding:utf-8 -*-
# _author:kaliarch
import requests
import urllib.parse
import time
import random
import hashlib
import json
class search(object):
def __init__(self):
self.url = ‘http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule‘
def getData(self,search_name):
# salt =i = "" + ((new Date).getTime() + parseInt(10 * Math.random(), 10)
salt = ((time.time() * 1000) + random.randint(1,10))
# sign = n.md5("fanyideskweb" + t + i + "ebSeFb%=XZ%T[KZ)c(sy!")
sign_text = "fanyideskweb" + search_name + str(salt) + "ebSeFb%=XZ%T[KZ)c(sy!"
sign = hashlib.md5((sign_text.encode(‘utf-8‘))).hexdigest()
paydata = {
‘i‘: search_name,
‘from‘: ‘AUTO‘,
‘to‘: ‘AUTO‘,
‘smartresult‘: ‘dict‘,
‘client‘: ‘fanyideskweb‘,
‘salt‘: salt,
‘sign‘: sign,
‘doctype‘: ‘json‘,
‘version‘: ‘2.1‘,
‘keyfrom‘: ‘fanyi.web‘,
‘action‘: ‘FY_BY_CLICKBUTTION‘,
‘typoResult‘: ‘false‘
}
return paydata
def getHeader(self):
header = {
‘Host‘: ‘fanyi.youdao.com‘,
‘Referer‘: ‘http://fanyi.youdao.com/‘,
‘Content-Type‘: ‘application/x-www-form-urlencoded; charset=UTF-8‘,
‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/55.0.2883.87 Safari/537.36‘,
‘Cookie‘: ‘[email protected]; OUTFOX_SEARCH_USER_ID_NCOO=129549097.60835753; UM_distinctid=15ff309f18ddc-094cb5494ad815-5d4e211f-1fa400-15ff309f18e449; _ga=GA1.2.184261795.1517119351; __guid=204659719.2556877880764680700.1518435624954.942; JSESSIONID=aaa3A5BLhtTrh4TPX_mgw; monitor_count=2; ___rl__test__cookies=1518488731567‘
}
return header
def getRequest(self,paydata,header):
_data = urllib.parse.urlencode(paydata).encode(‘utf-8‘)
_header = header
response = requests.post(self.url,data=_data,headers=_header)
return response.text
def getResult(self,response):
result_text = json.loads(response)
#src = result_text[‘translateResult‘][0][0][‘src‘]
tgt = result_text[‘translateResult‘][0][0][‘tgt‘]
return tgt
def main(self,search_name):
app = search()
paydata = app.getData(search_name)
header = app.getHeader()
response = app.getRequest(paydata, header)
tgt = app.getResult(response)
return tgt
windows.py代码如下:
#!/bin/env python
# -*- coding:utf-8 -*-
# _author:kaliarch
import tkinter as tk
from fanyi import search
class application:
def __init__(self):
self.windows = tk.Tk()
self.windows.title("翻译小工具")
self.windows.geometry("280x350+700+300")
#提交按钮
self.submit_btn = tk.Button(self.windows, text="查询",command=self.submit)
self.submit_btn.place(x=220, y=10, width=50, height=25)
# 定义输入框
self.entry = tk.Entry(self.windows)
self.entry.place(x=10, y=10, width=200, height=40)
#输出内容
self.result_text = tk.Text(self.windows, background="#ccc")
self.result_text.place(x=10, y=90, width=260, height=245)
# 翻译结果标题
self.title_label = tk.Label(self.windows, text="翻译结果:")
self.title_label.place(x=10, y=65)
self.search_result = search()
def submit(self):
#1.获取用户输入
context = self.entry.get()
#2.利用有道翻译
result = self.search_result.main(context)
#3.输出
self.result_text.delete(1.0,tk.END)
self.result_text.insert(tk.END,result)
def run(self):
self.windows.mainloop()
if __name__ == ‘__main__‘:
winapp = application()
winapp.run()
三、效果展示
运行windows.py,输入想要翻译的内容,点击翻译即可查看翻译结果
以上是关于Python实现翻译小工具的主要内容,如果未能解决你的问题,请参考以下文章