网址:https://fanyi.baidu.com/v2transapi?from=zh&to=en

解析流程

一个token和sign是我们需要解决的

image-20230517191509192

搜索sign:发现有好多个,都打上断点尝试一下

image-20230517192518225

断点到了这里

image-20230517192600112

直接把这个函数扣下来

image-20230517192756298

然后把根据报错把需要的给补上

另外token参数发现是固定可以不用解析,只要把sign解析出来即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function n(t, e) {
for (var n = 0; n < e.length - 2; n += 3) {
var r = e.charAt(n + 2);
r = "a" <= r ? r.charCodeAt(0) - 87 : Number(r),
r = "+" === e.charAt(n + 1) ? t >>> r : t << r,
t = "+" === e.charAt(n) ? t + r & 4294967295 : t ^ r
}
return t
}
var r = 320305.131321201

get_sign = function(t) {
var o, i = t.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
if (null === i) {
var a = t.length;
a > 30 && (t = "".concat(t.substr(0, 10)).concat(t.substr(Math.floor(a / 2) - 5, 10)).concat(t.substr(-10, 10)))
} else {
for (var s = t.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), c = 0, u = s.length, l = []; c < u; c++)
"" !== s[c] && l.push.apply(l, function(t) {
if (Array.isArray(t))
return e(t)
}(o = s[c].split("")) || function(t) {
if ("undefined" != typeof Symbol && null != t[Symbol.iterator] || null != t["@@iterator"])
return Array.from(t)
}(o) || function(t, n) {
if (t) {
if ("string" == typeof t)
return e(t, n);
var r = Object.prototype.toString.call(t).slice(8, -1);
return "Object" === r && t.constructor && (r = t.constructor.name),
"Map" === r || "Set" === r ? Array.from(t) : "Arguments" === r || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r) ? e(t, n) : void 0
}
}(o) || function() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
}()),
c !== u - 1 && l.push(i[c]);
var d = l.length;
d > 30 && (t = l.slice(0, 10).join("") + l.slice(Math.floor(d / 2) - 5, Math.floor(d / 2) + 5).join("") + l.slice(-10).join(""))
}
for (var p = "".concat(String.fromCharCode(103)).concat(String.fromCharCode(116)).concat(String.fromCharCode(107)), f = ["320305","131321201"], h = Number(f[0]) || 0, m = Number(f[1]) || 0, g = [], v = 0, y = 0; y < t.length; y++) {
var w = t.charCodeAt(y);
w < 128 ? g[v++] = w : (w < 2048 ? g[v++] = w >> 6 | 192 : (55296 == (64512 & w) && y + 1 < t.length && 56320 == (64512 & t.charCodeAt(y + 1)) ? (w = 65536 + ((1023 & w) << 10) + (1023 & t.charCodeAt(++y)),
g[v++] = w >> 18 | 240,
g[v++] = w >> 12 & 63 | 128) : g[v++] = w >> 12 | 224,
g[v++] = w >> 6 & 63 | 128),
g[v++] = 63 & w | 128)
}
for (var b = h, x = "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(97)) + "".concat(String.fromCharCode(94)).concat(String.fromCharCode(43)).concat(String.fromCharCode(54)), k = "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(51)) + "".concat(String.fromCharCode(94)).concat(String.fromCharCode(43)).concat(String.fromCharCode(98)) + "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(102)), _ = 0; _ < g.length; _++)
b = n(b += g[_], x);
return b = n(b, k),
(b ^= m) < 0 && (b = 2147483648 + (2147483647 & b)),
"".concat((b %= 1e6).toString(), ".").concat(b ^ h)
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import requests
import execjs

word = "时间"
url = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en'

with open('13.百度翻译2.js', encoding='utf-8') as js:
js_content = js.read()
compile = execjs.compile(js_content)
sign = compile.call('get_sign', word)

headers = {
'Cookie': '补上',
'Referer': 'https://fanyi.baidu.com/?aldtype=16047',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.71',
}

data = {
'from': 'zh',
'to': 'en',
'query': word,
'simple_means_flag': '3',
'sign': sign,
'token': 'f84f349ac4e1f7d82418c6922ccf5b1e',
'domain': 'common',
}

response = requests.post(url, headers=headers, data=data)
dst = response.json()['trans_result']['data'][0]['dst']
print(dst)