网页分析
目标网站:https://fanyi.youdao.com/
进入NetWork后,找到对应的Request URL 因为有道是进行了JS加密的,所以这里我们要注意表单,然后分析表单中的一些加密信息。 同样的通过搜索找到相应的js文件,找到对应的值和加密方法。
r:时间戳,注意保留的位数
i:是r加上[0,9]之间的一个随机数
t:浏览器版本的md5加密
sign:对"fanyideskweb" + e + i + "Y2FYu%TNSbMCxc3t2u^XT"这一串数据的md5加密
代码实现
1.引入库
import requests
import time
import random
import hashlib
2.实现翻译
word = input("please input:")
def make_md5(string):
string = string.encode('utf-8')
md5 = hashlib.md5(string).hexdigest()
return md5
lts = str(int(time.time() * 1000))
print(lts)
salt = lts + str(random.randint(0, 9))
print(salt)
sign =make_md5("fanyideskweb" + word + salt + "Y2FYu%TNSbMCxc3t2u^XT")
print(sign)
bv = make_md5('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36')
data = {
'i':word,
'from':'AUTO',
'to':'AUTO',
'smartresult':'dict',
'client':'fanyideskweb',
'salt':salt,
'sign':sign,
'lts':lts,
'bv':bv,
'doctype':'json',
'version':'2.1',
'keyfrom':'fanyi.web',
'action':'FY_BY_REALTlME',
}
headers = {
'Accept':'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding':'gzip, deflate, br',
'Accept-Language':'zh-CN,zh;q=0.9',
'Cache-Control':'no-cache',
'Connection':'keep-alive',
'Content-Length':'236',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie':'OUTFOX_SEARCH_USER_ID=1461755990@10.108.160.102; JSESSIONID=aaa2DGpVmXzJm8iDcfcUx; OUTFOX_SEARCH_USER_ID_NCOO=1322657966.5601964; fanyi-ad-id=114757; fanyi-ad-closed=1; ___rl__test__cookies=1629977928463',
'Host':'fanyi.youdao.com',
'Origin':'https://fanyi.youdao.com',
'Pragma':'no-cache',
'Referer':'https://fanyi.youdao.com/',
'sec-ch-ua':'"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"',
'sec-ch-ua-mobile':'?0',
'Sec-Fetch-Dest':'empty',
'Sec-Fetch-Mode':'cors',
'Sec-Fetch-Site':'same-origin',
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
'X-Requested-With':'XMLHttpRequest'
}
response = requests.post('https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule',data=data,headers=headers).json()
response_trans = response['translateResult'][0][0]['tgt']
print(response)
print(response_trans)
这里一定要注意data和headers。
二、tkinter制作窗口
import tkinter as tk
root = tk.Tk()
root.geometry('600x300')
root.title('桌面翻译器2.0')
b1 = tk.Button(root,text='翻译',width=8,command=traslate)
b1.pack()
t1 = tk.Text(root)
t1.pack()
root.mainloop()
同时注意输入的word要替换成 t1.get(0.0,‘end’) 完整代码如下:
import requests
import time
import random
import hashlib
import tkinter as tk
def make_md5(string):
string = string.encode('utf-8')
md5 = hashlib.md5(string).hexdigest()
return md5
def traslate():
lts = str(int(time.time() * 1000))
print(lts)
salt = lts + str(random.randint(0, 9))
print(salt)
sign =make_md5("fanyideskweb" + t1.get(0.0,'end') + salt + "Y2FYu%TNSbMCxc3t2u^XT")
print(sign)
bv = make_md5('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36')
data = {
'i':t1.get(0.0,'end'),
'from':'AUTO',
'to':'AUTO',
'smartresult':'dict',
'client':'fanyideskweb',
'salt':salt,
'sign':sign,
'lts':lts,
'bv':bv,
'doctype':'json',
'version':'2.1',
'keyfrom':'fanyi.web',
'action':'FY_BY_REALTlME',
}
headers = {
'Accept':'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding':'gzip, deflate, br',
'Accept-Language':'zh-CN,zh;q=0.9',
'Cache-Control':'no-cache',
'Connection':'keep-alive',
'Content-Length':'236',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie':'OUTFOX_SEARCH_USER_ID=1461755990@10.108.160.102; JSESSIONID=aaa2DGpVmXzJm8iDcfcUx; OUTFOX_SEARCH_USER_ID_NCOO=1322657966.5601964; fanyi-ad-id=114757; fanyi-ad-closed=1; ___rl__test__cookies=1629977928463',
'Host':'fanyi.youdao.com',
'Origin':'https://fanyi.youdao.com',
'Pragma':'no-cache',
'Referer':'https://fanyi.youdao.com/',
'sec-ch-ua':'"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"',
'sec-ch-ua-mobile':'?0',
'Sec-Fetch-Dest':'empty',
'Sec-Fetch-Mode':'cors',
'Sec-Fetch-Site':'same-origin',
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
'X-Requested-With':'XMLHttpRequest'
}
response = requests.post('https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule',data=data,headers=headers).json()
response_trans = response['translateResult'][0][0]['tgt']
print(response)
t1.delete(0.0,'end')
t1.insert(0.0,response_trans)
root = tk.Tk()
root.geometry('600x300')
root.title('桌面翻译器2.0')
b1 = tk.Button(root,text='翻译',width=8,command=traslate)
b1.pack()
t1 = tk.Text(root)
t1.pack()
root.mainloop()
|