1.打开有道在线翻译,并请求翻译
1.1获取翻译接口
1.2两次翻译数据对比
第一次
第二次
总结:
我们发现请求数据有差异:
salt,ts,sign,并且可以看到i对应翻译词组
2.查找js处理逻辑
2.1查看对应js
2.2处理逻辑
2.3python处理
word = "test"
import hashlib
import time
import random
# var t = n.md5(navigator.appVersion),对应浏览器版本
t = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"
bv = hashlib.md5(bytes(t,encoding="utf-8")).hexdigest()
#b4046c447614dcbe06d949fe3062a42a
r = str(int(round(time.time(),3)*1000))
ts = r #1585970420994
print(ts)
i = ts + str(random.randint(1,10))
salt = i #15859705441292
sign = hashlib.md5(bytes("fanyideskweb" + word + salt +"Nw(nmmbP%A-r6U3EUn]Aj",encoding="utf-8")).hexdigest()
print(sign) #582d24148760d00627461c99907b9476
3请求测试
import requests
import hashlib
import time
import random# var t = n.md5(navigator.appVersion),对应浏览器版本
t = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"
def get_auth_data(keyword):bv = hashlib.md5(bytes(t,encoding="utf-8")).hexdigest()#b4046c447614dcbe06d949fe3062a42a# 70244e0061db49a9ee62d341c5fed82ar = str(int(round(time.time(),3)*1000))ts = r #1585970420994i = ts + str(random.randint(1,10))salt = i #15859705441292sign = hashlib.md5(bytes("fanyideskweb" + keyword + salt +"Nw(nmmbP%A-r6U3EUn]Aj",encoding="utf-8")).hexdigest()return bv,ts,salt,signdef get_init_cookies():headers = {"User-Agent": t}res = requests.get("http://fanyi.youdao.com/",headers=headers)cookie_list = []for cookie in res.cookies:cookie_list.append("%s=%s"%(cookie.name,cookie.value))return ";".join(cookie_list)def translate(keyword):bv,ts,salt,sign = get_auth_data(keyword)cookies = get_init_cookies()url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"headers = {"Content-Type": "application/x-www-form-urlencoded","User-Agent": t,"Referer":"http://fanyi.youdao.com/","Cookie":cookies}data = {"i": keyword,"from": "zh-CHS","to": "en","smartresult": "dict","client": "fanyideskweb","salt": salt,"sign": sign,"ts": ts,"bv": bv,"doctype": "json","version": "2.1","keyfrom": "fanyi.web","action": "Y_BY_CLICKBUTTION"}res = requests.post(url,params=data,headers=headers)print(res.text)translate("测试")
返回:
{
“translateResult”: [[{
“tgt”: “test”,
“src”: “测试”
}
]],
“errorCode”: 0,
“type”: “zh-CHS2en”,
“smartResult”: {
“entries”: ["", “[试验] test\r\n”, “measurement\r\n”],
“type”: 1
}
}