url地址:http://fanyi.youdao.com/
综合上述分析,当改变里面内容时,Form Data(向后台发送的数据)中salt、sign、ts的值在变化,其他几个没有变化
链接地址为:http://shared.ydstatic.com/fanyi/newweb/v1.0.14/scripts/newweb/fanyi.min.js
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import requests,time,hashlib,random
def translate(text):
'''
加密:sign: n.md5("fanyideskweb" + e + i + "p09@Bn{h02_BIEe]$P^nG")
e:文本内容
i:时间戳
'''
e = text
i = int(time.time() * 1000) + random.randint(1, 10) #构建salt
r,y = divmod(i,10) #根据salt构建ts,也就是r
#构建签名
sign = "fanyideskweb" + e + str(i) + "p09@Bn{h02_BIEe]$P^nG"
md5 = hashlib.md5()
md5.update(sign.encode('utf-8'))
data = {
"i": text,
"from": "AUTO",
"to": "AUTO",
"smartresult": "dict",
"client": "fanyideskweb",
"salt": str(i),
"sign": md5.hexdigest(),
"ts":str(r),
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"action": "FY_BY_CLICKBUTTION",
"typoResult": "false"
}
url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
"Referer": "http://fanyi.youdao.com/",
"Cookie": "[email protected]; OUTFOX_SEARCH_USER_ID_NCOO=878094596.2005905; _ntes_nnid=6783bf0af2ef0809cfacae85efdafde4,1533193798143; JSESSIONID=aaaxTWFTX_yxR8SbjAwEw; ___rl__test__cookies=1544417009120",
}
response = requests.post(url=url, headers=headers, data=data).json()
result = response['translateResult'][0][0]['tgt']
print(result)
translate('故乡')
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from tkinter import *
import requests,time,hashlib,random
def translate():
'''
加密:sign: n.md5("fanyideskweb" + e + i + "p09@Bn{h02_BIEe]$P^nG")
e:文本内容
i:时间戳
'''
entry1.delete(0, END)
text = entry.get()
e = text
i = int(time.time() * 1000) + random.randint(1, 10) #构建salt
r,y = divmod(i,10) #根据salt构建ts,也就是r
#构建签名
sign = "fanyideskweb" + e + str(i) + "p09@Bn{h02_BIEe]$P^nG"
md5 = hashlib.md5()
md5.update(sign.encode('utf-8'))
data = {
"i": text,
"from": "AUTO",
"to": "AUTO",
"smartresult": "dict",
"client": "fanyideskweb",
"salt": str(i),
"sign": md5.hexdigest(),
"ts":str(r),
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"action": "FY_BY_CLICKBUTTION",
"typoResult": "false"
}
url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
"Referer": "http://fanyi.youdao.com/",
"Cookie": "[email protected]; OUTFOX_SEARCH_USER_ID_NCOO=878094596.2005905; _ntes_nnid=6783bf0af2ef0809cfacae85efdafde4,1533193798143; JSESSIONID=aaaxTWFTX_yxR8SbjAwEw; ___rl__test__cookies=1544417009120",
}
response = requests.post(url=url, headers=headers, data=data).json()
result = response['translateResult'][0][0]['tgt']
rest.set(result)
def youdao_translate():
# 创建窗口
root = Tk()
# 窗口标题
root.title("中英互译")
# 窗口大小、小写x
root.geometry('390x100+500+300')
# 标签控件
lable = Label(root, text="请输入要翻译的内容:", font=("微软雅黑"), fg="red")
lable.grid()
lablel1 = Label(root, text="翻译后的内容为:", font=("微软雅黑"), fg="green")
lablel1.grid()
global rest
rest = StringVar()
# 输入控件
global entry
entry = Entry(root, font=("微软雅黑", 14))
entry.grid(row=0, column=1)
# 翻译之后的结果
global entry1
entry1 = Entry(root, font=("微软雅黑", 14), textvariable=rest)
entry1.grid(row=1, column=1)
# 按钮控件 sticky对齐方式 N S E W - 上下左右
button = Button(root, text="翻译", font=("微软雅黑", 13), command=translate)
button.grid(row=2, column=0, sticky=W,padx=10)
button1 = Button(root, text="退出", font=("微软雅黑", 13), command=root.quit)
button1.grid(row=2, column=1, sticky=E,padx=10)
# 消息循环、显示窗口
root.mainloop()
if __name__ == '__main__':
youdao_translate()