import time
import hashlib
import requests
import subprocess
from functools import partial
subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')
#解决调用js时报错
import execjs
def spide_youdao(key):
session = requests.session()
session.get(url="https://fanyi.youdao.com/")
params = {
'i': key,
'from': 'auto',
'domain': 0,
'dictResult': 'true',
'keyid': 'webfanyi',
'sign': '',
'client': 'fanyideskweb',
'product': 'webfanyi',
'appVersion': '1.0.0',
'vendor': 'web',
'pointParam': 'client,mysticTime,product',
'mysticTime': 1695002145650,
'keyfrom': 'fanyi.web'
}
headers = {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded",
"Referer": "https://fanyi.youdao.com/"
}
t = str(int(time.time() * 1000))
s = f"client=fanyideskweb&mysticTime={t}&product=webfanyi&key=fsdsogkndfokasodnaso"
md5 = hashlib.md5()
md5.update(s.encode())
sign = md5.hexdigest()
params['sign'] = sign
params['mysticTime'] = t
resp = session.post(url="https://dict.youdao.com/webtranslate", data=params, headers=headers)
with open('sign.js', "r", encoding="utf-8") as f:
data = f.read()
js = execjs.compile(data)
return js.call("s", resp.text)
对应 sign.js 中的代码
const crypto = require('crypto');
function g(e) {
return crypto.createHash("md5").update(e).digest()
}
function s(t) {
let o = 'ydsecret://query/key/B*RGygVywfNBwpmBaZg*WT7SIOUP2T0C9WHMZN39j^DAdaZhAnxvGcCY6VYFwnHl'
let n = 'ydsecret://query/iv/C@lZe2YzHtZ2CYgaXKSVfsb7Y4QWHjITPPZ0nQp87fBeJ!Iv6v^6fvi2WN@bYpJ4'
const a = Buffer.alloc(16, g(o))
, c = Buffer.alloc(16, g(n))
, i = crypto.createDecipheriv("aes-128-cbc", a, c);
let s = i.update(t, "base64", "utf-8");
return s += i.final("utf-8")
}
测试结果如下:
print(spide_youdao("有道翻译"))
{"code":0,"dictResult":{"ce":{"word":{"trs":[{"voice":"Youdao+Translator&type=2","#text":"Youdao Translator","#tran":"有道翻译,有道翻译官;"}],"return-phrase":"有道翻译"}}},"translateResult":[[{"tgt":"Youdao Translation","src":"有道翻译","srcPronounce":"yŏu dào fānyì"}]],"type":"zh-CHS2en"}
标签:翻译,有道,youdao,js,import,sign,md5
From: https://www.cnblogs.com/xchuan/p/17712495.html