以下代码仅供学习和参考,如有侵权请联系删除
有道词典的解密说简单也很简单,说难也难。简单是js的解密简单,不像google一样复杂, 难是难在反爬虫上
请求的url要用这个链接进行post http://nmt.youdao.com/translate?smartresult=dict&smartresult=rule
一般新手小白可能就开始在{"errorCode":50} 上面了,以下代码可以直接复制粘贴使用,做个记录
import hashlib,time,random
import requests
def hashlibsalt(data):
e = "5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36"
t=hashlib.md5(e.encode(encoding='UTF-8')).hexdigest()
r=str(int(time.time()*1000))
i = r+str(int(10*random.random()))
return {
"ts":r,
"bv":t,
"salt":i,
"sign":hashlib.md5(("fanyideskweb" + data + i + "Y2FYu%TNSbMCxc3t2u^XT").encode(encoding='UTF-8')).hexdigest()
}
def translate(sentence):
try:
url ="http://nmt.youdao.com/translate?smartresult=dict&smartresult=rule"
headers = {
"Cookie": "OUTFOX_SEARCH_USER_ID=1076696971@10.168.8.63;JSESSIONID=aaaZ6s5m9DVmYf8g3QoDw;OUTFOX_SEARCH_USER_ID_NCOO=473292646.5080033;___rl__test__cookies=1543228484656",
"Host": "fanyi.youdao.com",
"Origin":"http://fanyi.youdao.com",
"Referer":"http://fanyi.youdao.com/",
"User-Agent": "Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/69.0.3497.100Safari/537.36OPR/56.0.3051.104X-Requested-With:XMLHttpRequest"}
r = hashlibsalt(sentence)
data = {
"i":sentence,
"from": "AUTO",
"to": "AUTO",
"smartresult": "dict",
"client":"fanyideskweb",
"salt": r['salt'],
"sign": r['sign'],
"lts": r['ts'],
"bv": r['bv'],
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"action": "FY_BY_REALTIME"
}
res = requests.post(url,headers=headers,data=data).json()
if res['errorCode'] !=50:
print(res['translateResult'][0][0]['tgt'])
return res['translateResult'][0][0]['tgt']
else:
return ''
except:
return 'err'
translate("你好,世界上最好的语言")
|