Python实现翻译小工具
一、背景
利用requests模块获取有道词典web页面的post信息,beautifulsoup来获取需要的内容,通过tkinter模块生成gui界面。
二、代码
git源码地址
python实现翻译小工具
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 = ‘’
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': 'outfox_search_user_id=-846616837@1.80.219.201; 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
- 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
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()
- 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
if name == ‘main’:
winapp = application()
winapp.run()
setup.py代码如下:
-- coding:utf-8 --
_author:kaliarch
import sys
from cx_freeze import setup,executable
import os
os.environ[‘tcl_library’] = r"c:\program files\python36\tcl\tcl8.6"
os.environ[‘tk_library’] = r"c:\program files\python36\tcl\tk8.6"
include_files = [
r"c:\program files\python36\dlls\tcl86t.dll",
r"c:\program files\python36\dlls\tk86t.dll",
]
build_exe_options = {
“packages”:[“os”,“tkinter”,“requests”,“idna”],
“include_files”:include_files
}
base = none
if sys.platform == “win32”:
base = “win32gui”
setup(name = “translate_tool”,
version = “0.1”,
description = “fanyitools!”,
options = {“build_exe”:build_exe_options},
executables = {executable(“”,base=base,icon=‘img.ico’)}
)
三、效果展示
,输入想要翻译的内容,点击翻译即可查看翻译结果
python实现翻译小工具
python实现翻译小工具
可以利用cx_freeze打包成windows的mis安装小程序,方便使用
python实现翻译小工具
切换到项目目录下执行python bdist_msi
待执行完毕,可以项目文件下生成两个文件夹dist中为msi安装文件,在其他windows服务器安装后就为build下的文件内容,在build下的exe.win-amd64-3.6下的windows.exe 就可打开小工具
python实现翻译小工具
进行安装测试
python实现翻译小工具
安装完成后可以运行安装目录下的windows.exe打开小工具
python实现翻译小工具
提高效率,同时也能打造属于自己的工具,>>>
活动地址>>>>
上一篇: Leetcode231. 2的幂
下一篇: Python画热图之seaborn