1、查看请求链接
2、请求方法是post,看表单
多次请求对比,发现只有sign是动态的。
3、找sign的计算方法
发现了sign,打一个断点,翻译请求后,程序就会在该断点处拿到e的值(效果如下图所示)
原来是一个函数e(r)计算出了sign,找到e(r)
复制这串代码,自定义为fanyi.js,放在Python文件下,等待Python调用。
4、js代码
代码内容会和你目前粘贴的有出入,因为e(r)函数,缺少参数r的定义,需要继续找到并加入。除此以外,参数i也要加入。
完整js代码如下:
function n(r, o) {
for (var t = 0; t < o.length - 2; t += 3) {
var a = o.charAt(t + 2);
a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a),
a = "+" === o.charAt(t + 1) ? r >>> a : r << a,
r = "+" === o.charAt(t) ? r + a & 4294967295 : r ^ a
}
return r
}
function e(r) {
var i = "320305.131321201"
var o = r.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
if (null === o) {
var t = r.length;
t > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(t / 2) - 5, 10) + r.substr(-10, 10))
} else {
for (var e = r.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), C = 0, h = e.length, f = []; h > C; C++)
"" !== e[C] && f.push.apply(f, a(e[C].split(""))),
C !== h - 1 && f.push(o[C]);
var g = f.length;
g > 30 && (r = f.slice(0, 10).join("") + f.slice(Math.floor(g / 2) - 5, Math.floor(g / 2) + 5).join("") + f.slice(-10).join(""))
}
var u = void 0
, l = "" + String.fromCharCode(103) + String.fromCharCode(116) + String.fromCharCode(107);
u = null !== i ? i : (i = window[l] || "") || "";
for (var d = u.split("."), m = Number(d[0]) || 0, s = Number(d[1]) || 0, S = [], c = 0, v = 0; v < r.length; v++) {
var A = r.charCodeAt(v);
128 > A ? S[c++] = A : (2048 > A ? S[c++] = A >> 6 | 192 : (55296 === (64512 & A) && v + 1 < r.length && 56320 === (64512 & r.charCodeAt(v + 1)) ? (A = 65536 + ((1023 & A) << 10) + (1023 & r.charCodeAt(++v)),
S[c++] = A >> 18 | 240,
S[c++] = A >> 12 & 63 | 128) : S[c++] = A >> 12 | 224,
S[c++] = A >> 6 & 63 | 128),
S[c++] = 63 & A | 128)
}
for (var p = m, F = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(97) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(54)), D = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(51) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(98)) + ("" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(102)), b = 0; b < S.length; b++)
p += S[b],
p = n(p, F);
return p = n(p, D),
p ^= s,
0 > p && (p = (2147483647 & p) + 2147483648),
p %= 1e6,
p.toString() + "." + (p ^ m)
}
5、请求、调用的代码
import requests
from fake_useragent import UserAgent
import execjs
class BaiduFanyi():
def __init__(self, query):
self.url = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en'
self.query = query
self.headers = {
'User-Agent': UserAgent(verify_ssl=False).random
}
self.cookies = {
'BAIDUID_BFESS': '9254CAD7BD9966315CDDC6D2931CD05A:FG=1',
'BAIDUID': '9254CAD7BD996631B92D894CD3A63CDF:FG=1',
'RT': '"z=1&dm=baidu.com&si=uktj2p1d5o&ss=l10s06iu&sl=4&tt=2zx&bcn=https://fclog.baidu.com/log/weirwood?type=perf&ld=c6t&cl=awu&ul=7hjv&hd=7hlz"',
'REALTIME_TRANS_SWITCH': '1',
'FANYI_WORD_SWITCH': '1',
'HISTORY_SWITCH': '1',
'SOUND_SPD_SWITCH': '1',
'SOUND_PREFER_SWITCH': '1',
'APPGUIDE_10_0_2': '1',
'Hm_lvt_64ecd82404c51e03dc91cb9e8c025574': '1648218223,1648262246',
'Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574': '1648278724',
'ab_sr': '1.0.1_MDY5OWNlNmNmNjllNGIyNmEyN2MyYWQ4MDUyOGNhMjY1ZThkNzEyMWZjNWZiNjA3YWNhMjRhMjUwYmYwYmMwYjE0ZGI5OTY5Yjc3ODNhZDFkOTY3ODBiNGJlN2NiN2EyZjIyZGVjZGU5ZjgyMWI1ZDRjZmIwZDZmMDM2MDBjMjJiNjNiNWY4ZTFkNjg5ZTE1NWQ4ZWFkOTc3ZWU1YjhiZQ=='}
def make_data(self):
data = {
'from': 'zh',
'to': 'en',
'query': self.query,
'transtype': 'realtime',
'simple_means_flag': '3',
'sign': self.make_sign(),
'token': '96d82a9f5a76b7954d06761f3f115609',
'domain': 'common'
}
return data
def make_sign(self):
with open('D:/baidu_fanyi/fanyi.js', 'r', encoding='utf-8') as f:
sign = execjs.compile(f.read()).call('e', self.query)
return sign
def get_content(self):
response = requests.post(url=self.url, data=self.make_data(), headers=self.headers, cookies=self.cookies).json()
translate = response['trans_result']['data'][0]['dst']
print('translate:' + translate)
if __name__=='__main__':
fanyi = BaiduFanyi('金融')
fanyi.get_content()
|