1. 源码
import requests
import time
import random
import hashlib
import logging
import re
import js2py
import urllib3
from bs4 import BeautifulSoup
import tkinter as tk
from tkinter import *
from tkinter import ttk
import tkinter
from tkinter.ttk import Button
import threading
from PIL import Image,ImageTk
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"
def show_request_info(func):
def inner(word,*o):
res = func(word, *o)
logger.info("URL:%s"%res.url)
logger.info("Http code:%d"%res.status_code)
logger.info("Response content:%s"%res.text)
res_dict= res.json()
if "translateResult" in res_dict.keys():
result_list = []
for info in res_dict["translateResult"]:
result_list.append(info[0]["tgt"])
return "\n".join(result_list)
elif "trans_result" in res_dict.keys():
result_list = []
for info in res_dict["trans_result"]["data"]:
result_list.append(info["dst"])
return "\n".join(result_list)
elif len(res_dict.keys())==3 or len(res_dict.keys())==2:
return res_dict["result"]
else:
return "翻译失败了,请稍后再试。"
return inner
class YouDaoFanYi:
def __init__(self):
self.bv = "75a84f6fbcebd913f0a4e81b6ee54608"
self.cookies = self.get_cookie()
self.headers = {"Host": "fanyi.youdao.com",
"User-Agent": user_agent,
"Content-Type": "application/x-www-form-urlencoded",
"Referer": "http://fanyi.youdao.com/",
"Origin": "http://fanyi.youdao.com",
"Host": "fanyi.youdao.com"}
self.session = requests.Session()
self.session.headers.update(self.headers)
self.session.cookies.update(self.cookies)
def get_salt(self, ts):
salt = ts + random.randint(0,9)
return salt
def get_md5(self, value):
md5 = hashlib.md5()
md5.update(value.encode('utf-8'))
value = md5.hexdigest()
return value
def get_sign(self, word, salt):
sign_str = "fanyideskweb" + word + str(salt) + "n%A-rKaT5fb[Gy?;N5@Tj"
sign = self.get_md5(sign_str)
return sign
def get_cookie(self):
headers = {"User-Agent": user_agent}
res = requests.get("http://fanyi.youdao.com/", headers=headers)
return res.cookies
@show_request_info
def translate_auto(self, word):
ts = int(time.time()*1000)
salt = self.get_salt(ts)
sign = self.get_sign(word, salt)
inputData = {"i": word,
"from": "AUTO",
"to": "AUTO",
"smartresult": "dict",
"client": "fanyideskweb",
"salt": str(salt),
"sign": sign,
"ts": ts,
"bv": self.bv,
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"action": "FY_BY_CLICKBUTTION"}
res = self.session.post("http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule", data=inputData)
assert res.status_code==200
return res
@show_request_info
def translate(self, word, from_language, to_language):
ts = int(time.time()*1000)
salt = self.get_salt(ts)
sign = self.get_sign(word, salt)
language_dict = {"中文":"zh-CHS",
"英文":"en",
"韩文":"ko",
"日文":"ja"}
from_language = language_dict[from_language]
to_language = language_dict[to_language]
inputData = {"i": word,
"from": from_language,
"to": to_language,
"smartresult": "dict",
"client": "fanyideskweb",
"salt": str(salt),
"sign": sign,
"ts": ts,
"bv": self.bv,
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"action": "FY_BY_CLICKBUTTION"}
res = self.session.post("http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule", data=inputData, headers=self.headers, cookies=self.cookies)
assert res.status_code==200
return res
@show_request_info
def translate_general(self, word):
headers = {"User-Agent": user_agent,
"Content-Type": "application/x-www-form-urlencoded"}
data = {"i":word,
"doctype":"json"}
res = requests.post("http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule", data=data, headers=headers)
assert res.status_code==200
return res
class BaiDuFanYi:
def __init__(self):
self.content = None
self.headers = {"User-Agent": user_agent,
"Content-Type": "application/x-www-form-urlencoded"}
self.cookies, self.token= self.get_cookies_and_token()
self.session = requests.Session()
self.session.headers.update(self.headers)
self.session.cookies.update(self.cookies)
def get_cookies_and_token(self):
headers = {"User-Agent": user_agent}
res= requests.get("https://fanyi.baidu.com/", verify=False, timeout=10)
assert res.status_code==200
cookies = res.cookies
time.sleep(1)
headers["Content-Type"] = "application/x-www-form-urlencoded"
res= requests.get("https://fanyi.baidu.com/", headers=headers, cookies=cookies, verify=False)
content =res.text
token = re.findall("token: '(.*)'", content)[0]
assert res.status_code==200
self.content = res.text
return cookies, token
def get_sign(self, word):
gtk = re.findall(r";window.gtk = ('.*')", self.content)[0]
js = r"""
function n(r, o) {
for (var t = 0; t < o.length - 2; t += 3) {
var a = o.charAt(t + 2);
a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a), a = "+" === o.charAt(t + 1) ? r >>> a : r << a, r = "+" === o.charAt(t) ? r + a & 4294967295 : r ^ a
}
return r
}
function e(r) {
var o = r.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
if (null === o) {
var t = r.length;
t > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(t / 2) - 5, 10) + r.substr(-10, 10))
} else {
for (var e = r.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), C = 0, h = e.length, f = []; h > C; C++) "" !== e[C] && f.push.apply(f, a(e[C].split(""))), C !== h - 1 && f.push(o[C]);
var g = f.length;
g > 30 && (r = f.slice(0, 10).join("") + f.slice(Math.floor(g / 2) - 5, Math.floor(g / 2) + 5).join("") + f.slice(-10).join(""))
}
var u = void 0, test_socket = "" + String.fromCharCode(103) + String.fromCharCode(116) + String.fromCharCode(107);
u = null !== i ? i : (i = window[test_socket] || "") || "";
for (var d = u.split("."), m = Number(d[0]) || 0, s = Number(d[1]) || 0, S = [], c = 0, v = 0; v < r.length; v++) {
var A = r.charCodeAt(v);
128 > A ? S[c++] = A : (2048 > A ? S[c++] = A >> 6 | 192 : (55296 === (64512 & A) && v + 1 < r.length && 56320 === (64512 & r.charCodeAt(v + 1)) ? (A = 65536 + ((1023 & A) << 10) + (1023 & r.charCodeAt(++v)), S[c++] = A >> 18 | 240, S[c++] = A >> 12 & 63 | 128) : S[c++] = A >> 12 | 224, S[c++] = A >> 6 & 63 | 128), S[c++] = 63 & A | 128)
}
for (var p = m, F = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(97) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(54)), D = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(51) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(98)) + ("" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(102)), b = 0; b < S.length; b++) p += S[b], p = n(p, F);
return p = n(p, D), p ^= s, 0 > p && (p = (2147483647 & p) + 2147483648), p %= 1e6, p.toString() + "." + (p ^ m)
}
"""
js = js.replace(r'null !== i ? i : (i = window[test_socket] || "") || ""', gtk)
context = js2py.EvalJs()
context.execute(js)
sign = context.e(word)
return sign
@show_request_info
def translate(self, word, from_language, to_language):
language_dict = {"中文":"zh",
"英文":"en",
"韩文":"kor",
"日文":"jp"}
from_language = language_dict[from_language]
to_language = language_dict[to_language]
sign = self.get_sign(word)
data = {
'from': from_language,
'to': to_language,
'query': word,
"transtype":"translang",
"simple_means_flag":3,
'sign': sign,
'token': self.token,
"domain":"common"
}
res = self.session.post("https://fanyi.baidu.com/v2transapi?from=en&to=zh", data=data, verify=False)
assert res.status_code==200
return res
class Create_win:
def __init__(self):
self.master = self.create()
self.master.wm_resizable(False,False)
self.select_tool = tkinter.StringVar()
self.select_from_language = tkinter.StringVar()
self.select_to_language = tkinter.StringVar()
self.translat_frm = tk.Frame(self.master)
self.translat_input = Text(self.translat_frm,
width=65,
height=10,
font=('Arial', 12))
self.result_input = Text(self.translat_frm,
width=65,
height=10,
font=('Arial', 12))
try:
self.youdao_obj = YouDaoFanYi()
except:
self.result_input.insert(0.0, "连接有道翻译失败了,请重新点击刷新按钮.")
try:
self.baidu_obj = BaiDuFanYi()
except:
self.result_input.insert(0.0, "连接百度翻译失败了,请重新点击刷新按钮.")
def thread_it(self, func, *args):
'''将函数放入线程中执行'''
t = threading.Thread(target=func, args=args)
t.setDaemon(True)
t.start()
def create(self):
win = tk.Tk()
win.title("翻译")
win.geometry("600x500+300+200")
return win
def set_translat_tool(self):
translat_tool_frm = tk.Frame(self.master)
translat_tool_frm.grid(row=1)
ttk.Label(translat_tool_frm,text='请选择翻译工具').grid(row=0,column=0)
S = ttk.OptionMenu(translat_tool_frm, self.select_tool, "百度翻译", "百度翻译", "有道翻译")
S.grid(row=0,column=1)
def set_language(self):
set_language_frm = tk.Frame(self.master)
set_language_frm.grid(row=2)
ttk.Label(set_language_frm,text='请选择语言').grid(row=0,column=0)
S1 = ttk.OptionMenu(set_language_frm, self.select_from_language, "中文", "中文", "英文", "韩文","日文")
S1.grid(row=0,column=1)
S2 = ttk.OptionMenu(set_language_frm, self.select_to_language, "英文", "英文", "中文", "韩文","日文")
S2.grid(row=0,column=3)
exchange_language_button = Button(set_language_frm, text="<==>", command=lambda : self.thread_it(self.exchange_language))
exchange_language_button.grid(row=0,column=2)
def exchange_language(self):
from_language = self.get_select_from_language()
to_language = self.get_select_to_language()
self.select_from_language.set(to_language)
self.select_to_language.set(from_language)
def getValue(self):
print(self.select_tool.get())
def set_input_word(self):
self.translat_frm.grid(row=3)
ttk.Label(self.translat_frm,text='翻译语句').grid(row=1)
Button(self.translat_frm, text="刷新", command=lambda : self.thread_it(self.refresh_session)).grid(row=1,sticky=tk.E)
self.translat_input.grid(row=2)
scroll = Scrollbar(self.translat_frm, command=self.translat_input.yview)
scroll.grid(row = 2,sticky = tk.N + tk.S+tk.E)
self.translat_input.configure(yscrollcommand=scroll.set)
def refresh_session(self):
try:
self.youdao_obj = YouDaoFanYi()
except:
self.result_input.insert(0.0, "连接有道翻译失败了,请重新点击刷新按钮.")
try:
self.baidu_obj = BaiDuFanYi()
except:
self.result_input.insert(0.0, "连接百度翻译失败了,请重新点击刷新按钮.")
def set_translat_button(self):
translat_button = Button(self.translat_frm, text="翻译", command=lambda : self.thread_it(self.request_translat))
translat_button.grid(row=3, sticky=tk.E)
def set_result_input(self):
ttk.Label(self.translat_frm,text='翻译结果').grid(row=3)
self.result_input.grid(row=5)
scroll = Scrollbar(self.translat_frm, command=self.result_input.yview)
scroll.grid(row = 5,sticky = tk.N + tk.S+tk.E)
self.result_input.configure(yscrollcommand=scroll.set)
def get_current_tool(self):
return self.select_tool.get()
def get_select_from_language(self):
return self.select_from_language.get()
def get_select_to_language(self):
return self.select_to_language.get()
def request_translat(self):
self.result_input.delete(0.0, tkinter.END)
tool = self.get_current_tool()
word_list = self.translat_input.get(0.0,tkinter.END).strip().split("\n")
from_language = self.get_select_from_language()
to_language = self.get_select_to_language()
if ('' in word_list) and len(word_list)<=1:
self.result_input.insert(INSERT, "您没有输入翻译内容,请输入。")
else:
if tool=="百度翻译":
result = self.baidu_obj.translate("\n".join(word_list)+"\n", from_language, to_language)
self.result_input.insert(INSERT, result)
elif tool=="有道翻译":
result = self.youdao_obj.translate("\n".join(word_list), from_language, to_language)
self.result_input.insert(INSERT, result)
elif tool=="knoxPortal":
result_list = []
for word in word_list:
if word.isspace() or word=="":
pass
else:
temp = self.knoxPotal_obj.translate(word, from_language, to_language)
result_list.append(temp)
result = "\n".join(result_list)
self.result_input.insert(INSERT, result)
else:
pass
def run():
w = Create_win()
w.set_translat_tool()
w.set_language()
w.set_input_word()
w.set_result_input()
w.set_translat_button()
tk.mainloop()
if __name__=="__main__":
run()
2. 测试效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a7e967e75315aa7922d28cb6602d93e6.png)