|
发表于 2022-7-5 02:40:30
|
显示全部楼层
import requests
import hashlib
import time
import random
from jsonpath import jsonpath
import tkinter as tk
class FanYi:
def __init__(self, word, country):
self.url = 'https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36',
'Referer': 'https://fanyi.youdao.com/',
'Cookie': 'OUTFOX_SEARCH_USER_ID=1907954952@182.203.147.49; OUTFOX_SEARCH_USER_ID_NCOO=2032702153.1070416; fanyi-ad-id=306808; fanyi-ad-closed=1; ___rl__test__cookies=1654931146702',
}
self.word = word
self.country = country
def run(self):
lts = int(time.time()*1000)
salt = str(lts) + str(random.randint(0, 9))
sign = hashlib.md5(("fanyideskweb" + self.word + salt + "Ygy_4c=r#e#4EX^NUGUc5").encode()).hexdigest()
data = {
'i': self.word,
'from': 'zh-CHS',
'to': self.country,
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': salt,
'sign': sign,
'lts': lts,
'bv': '85618492e851e16ca0a03b8e100fdddb',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTlME'
}
response = requests.post(self.url, headers=self.headers, data=data).json()
text = jsonpath(response, '$..tgt')[0]
return text
root = tk.Tk()
root.title('简易小翻译')
root.geometry('300x150')
L1 = tk.Label(root, text='请输入要翻译的内容:', font=('宋体', 14)).pack(anchor='nw')
txt = tk.StringVar()
txt.set('')
E1 = tk.Entry(root, textvariable=txt, width=32, bd=3).pack(anchor='nw')
L2 = tk.Label(root, text='翻译后的内容:', font=('宋体', 14)).pack(anchor='nw')
txt2 = tk.StringVar()
def fun1():
word = txt.get()
test = FanYi(word, 'en')
res = test.run()
txt2.set(res)
def fun2():
word = txt.get()
test = FanYi(word, 'ja')
res = test.run()
txt2.set(res)
def fun3():
word = txt.get()
test = FanYi(word, 'ko')
res = test.run()
txt2.set(res)
def fun4():
word = txt.get()
test = FanYi(word, 'ru')
res = test.run()
txt2.set(res)
L3 = tk.Label(root, textvariable=txt2, font=('宋体', 14)).pack(anchor='nw')
B1 = tk.Button(root, text='中->英', command=fun1).pack(anchor='nw')
B2 = tk.Button(root, text='中->日', command=fun2).place(x=60, y=100)
B3 = tk.Button(root, text='中->韩', command=fun3).place(x=120, y=100)
B4 = tk.Button(root, text='中->俄', command=fun4).place(x=180, y=100)
root.mainloop()
-----------------------------------------------------------------------------------------------
有道翻译post请求里的form表单中的salt, sign, lts等几个参数也是随着每次请求不断变化的, 需要做逆向处理,
lts和salt跟时间戳有关, sign则是采用了MD5加密 |
|