python中使用md5 32位小写语法
from hashlib import md5
sign=md5((要加密的字符串).encode("utf-8")).hexdigest()
js文件
function mydate() {
r = "" + (new Date).getTime();
return r;
}
//盐:salt=mysalt(ts)
function mysalt(a) {
i = a+ parseInt(10 * Math.random(), 10);
return i;
}
py文件,? ? ? ?这里要注意user-agent中只截取一部分
import execjs
import requests
import urllib3
from hashlib import md5
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
header={
'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':'256',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie':'OUTFOX_SEARCH_USER_ID=-1314230209@10.110.96.158; OUTFOX_SEARCH_USER_ID_NCOO=1237104235.5745795; _ntes_nnid=9e5255dda0fb2eaeffa89ecda2e1b062,1648343512936; JSESSIONID=aaacZfeG0YHZze36Vfu-x; fanyi-ad-id=305366; fanyi-ad-closed=1; ___rl__test__cookies=1648530764694',
'Host':'fanyi.youdao.com',
'Origin':'https://fanyi.youdao.com',
'Pragma':'no-cache',
'Referer':'https://fanyi.youdao.com/',
'sec-ch-ua':'"Not A;Brand";v="99","Chromium";v="99","Google Chrome";v="99"',
'sec-ch-ua-mobile':'?0',
'sec-ch-ua-platform':'"Windows"',
'Sec-Fetch-Dest':'empty',
'Sec-Fetch-Mode':'cors',
'Sec-Fetch-Site':'same-origin',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
}
def structure_form(query):
"""
读取js文件
执行js代码得到我们苦求的sign值
构造新的表单
:return:
"""
a="5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36"
bv= md5(a.encode("utf-8")).hexdigest()
print("bv是"+bv)
print(header['User-Agent'])
with open('test.js', 'r', encoding='utf-8') as f:
ctx = execjs.compile(f.read())
ts=ctx.call('mydate')
print("ts是"+ts)
salt=ctx.call('mysalt',ts)
print("salt是"+salt)
print("fanyideskweb" + query + salt + "Ygy_4c=r#e#4EX^NUGUc5")
sign = md5(("fanyideskweb" + query + salt + "Ygy_4c=r#e#4EX^NUGUc5").encode("utf-8")).hexdigest()
print("sign"+sign)
# # sign成功获取,写入date
# print("bv是"+sign)
# return bv
data={
'i':query,
'from':'AUTO',
'to':'AUTO',
'smartresult':'dict',
'client':'fanyideskweb',
'salt':salt,
'sign':sign,
'lts':ts,
'bv':bv,
'doctype':'json',
'version':'2.1',
'keyfrom':'fanyi.web',
'action':'FY_BY_CLICKBUTTION'
}
print(data)
return data
if __name__=='__main__':
url='https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
query="坚持"
res = requests.post(url,data=structure_form(query),headers=header,verify=False).json()
print(res)
text=res["translateResult"][0][0]['src']
texttran=res["translateResult"][0][0]['tgt']
print(text+"翻译成"+texttran)
|