1. 抓包
2. 分析知道其中4个参数为加密数据,搜索其中一个参数salt
搜索到一个js文件,点击大括号格式化查看
查到4个参数
打断点分析可知道 ts是13位时间戳,salt是时间戳加上一位0到9的随机数,bv是user-agent的md5加密数据,sign是几个字符串相加起来的md5加密数据
代码实现
# coding:utf-8
import requests
import time
import random
import hashlib
import tkinter as tk
def md5_str(str):
return hashlib.md5(str.encode('utf-8')).hexdigest()
def translate():
url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'Connection': 'keep-alive',
'Content-Length': '238',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': '[email protected]; OUTFOX_SEARCH_USER_ID_NCOO=2137932096.4847515; _ntes_nnid=1cc93fcef522a0806d068fafcbe1fc88,1585712781607; P_INFO=the_dean; _ga=GA1.2.670589852.1587713815; JSESSIONID=aaa_BHBSDrSnl4w_cjWix; DICT_UGC=be3af0da19b5c5e6aa4e17bd8d90b28a|; JSESSIONID=abc_Qj-ZFXRHBbmjKkYix; ___rl__test__cookies=1589979656584',
'Host': 'fanyi.youdao.com',
'Origin': 'http://fanyi.youdao.com',
'Referer': 'http://fanyi.youdao.com/?keyfrom=dict2.top',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'
}
word = text.get(0.0, 'end') # 获取输入框的文本
ts = str(int(time.time() * 1000)) # 时间戳
salt = ts + str(random.randint(0, 9)) # 时间戳加0到9的随机数
sign = md5_str("fanyideskweb" + word + salt + "Nw(nmmbP%A-r6U3EUn]Aj")
bv = md5_str("5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36")
data = {
'i': word,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': salt,
'sign': sign,
'ts': ts,
'bv': bv,
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTlME'
}
response = requests.post(url, data=data, headers = headers)
result = response.json()['translateResult'][0][0]['tgt']
text.delete(0.0, 'end')
text.insert(0.0, result)
window = tk.Tk()
# 窗口大小
window.geometry('400x250')
window.title('翻译小工具')
# 点击按钮
button = tk.Button(window, text='翻译', command=translate, width=8)
button.pack()
# 输入框
text = tk.Text(window)
text.pack()
# 一直显示
window.mainloop()
Tkinter图形界面