打开,开发者工具分析网页
发现响应的数据就在这个请求里面。那我们就对这接口发起请求。这里要携带cookie ,在看看FromData携带的参数内容要对sign进行JS逆向不然请求不到数据。 sign是个什么东西看不懂,我们去js里面找。
创建一个js文件,把复制的代码贴进去
创建一个爬虫文件,导入第三方库进行JS逆向
**安装第三方库:pip install PyExecJS
import requests
import execjs
def get_sign (wrod):
with open('test.js','r',encoding='utf-8')as f:
jscode = f.read()
sign = execjs.compile(jscode).call('e',wrod)
return sign
def request(wrod):
sign = get_sign(wrod)
url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'
headers = {
"Cookie":"PSTM=1632138258; BIDUPSID=B1DCF4AAEB5CD472B2A25470A4D5DF7E; BAIDUID=8EE56E92CF9DB36CC92881E8ECE04C80:FG=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BAIDUID_BFESS=D3C9A35CDAFA96EFE88CE478AB6C913E:FG=1; __yjs_duid=1_6f481431e087be11f111b85c844be8611632668566599; delPer=0; PSINO=7; BCLID=11794686336521135670; BDSFRCVID=o20OJexroG0YyvRH_rxNUryx4uweG7bTDYLEOwXPsp3LGJLVJeC6EG0Pts1-dEu-EHtdogKKKmOTHc0F_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=JRAjoK-XJDvDqTrP-trf5DCShUFs0U3RB2Q-XPoO3KO4VMI6yM_KW--AKlrOtPbiW5FeafbgylRM8P3y0bb2DUA1y4vpKh5ma2TxoUJ2XMcZ8Jjcqq7ohb8ebPRiB-r9Qgb2opQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0MD8xDT8Bj6PVKgTa54cbb4o2WbCQLIJm8pcN2b5oQTtZ0bufBULHtnvPQtTyJn3vOPLRylOUWfA3XpJvQnJjt2JxaqRC5MQTDp5jDh3MBpI4XfQWelvyaTRy0hvcWb6cShnnDMjrDRLbXU6BK5vPbNcZ0l8K3l02V-bIe-t2XjQhjH-jJ508JJ3aQ5rtKRTffjrnhPF30To3X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RNB-3iV-OxDUvnyxAZyxomtfQxtNRJQKDE5p5hKq5S5-OobUPUyUc9LUv2tgcdot5yBbc8eIna5hjkbfJBQttjQn3hfIkj2CKLK-oj-DD4jTDh3j; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1632817849; FANYI_WORD_SWITCH=1; REALTIME_TRANS_SWITCH=1; SOUND_PREFER_SWITCH=1; SOUND_SPD_SWITCH=1; HISTORY_SWITCH=1; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1632822799; ab_sr=1.0.1_NDM2NTA4ODlhZDFjOWI5YjVlMDFmMTA3NTUyZWQyYjVhYjliMTZhZmRkMjcwMzIyNTlkZTc0NTJhYTQwN2MxZjlkY2YyMDBlMTIwMGU3Nzk3YmRmYTlhNDA4Yzg4Njc5ZDVlNTMwNzNlNmIzNDg3YjZiNWExNjc0ODE2YzA3NjQyMThkNGMxNzk0YTk2MTgxNTBmMjQ0NGI5MDdhZTZmYQ==; __yjs_st=2_MjQxNjQxNjlhMThlZTA0ZTFmZjIwYTdlN2NiMjAzNTVjOTU1NmU5YWFlYmM2NGY1Y2VlZGJhZTQ5YzUzNGNjZDcwZjYwODVmMjBjZWFlMGRiZWE5ZWY5MWQxMmZlOTIzNzA1OWU3NGNiYTMxYTUxNDQ0OTA1ZTY1ZmQ0ZDRjYWJlMzhjYjBhOTYyZTc1OWFmNDY0YTdjYmU1NjFjOTFhZTk0MGI0M2I2NDQ5OGVkNTZjZjlhNmRlYTQwOTg2NmZjMzVmYTA2OTI1MWQ0MDZmYTJlOGQ4ODExOGI1OGUzMDUxZTMyOGRmNDhhYzVlY2I5MGQxMzIyOGM5YTY5NmEzMDAyNDk2NjI2ODcxYjU4NmQzYWIzZGRlNDAwMWJiNmYzXzdfZWJjMzMxMDk=; H_PS_PSSID=34650_34068_34748_34551_34524_34584_34107_26350_34691_34682; BA_HECTOR=ah202h2kah0lak84o11gl5pmg0q",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36"
}
data = {
'from': 'en',
'to': 'zh',
'query':wrod,
'transtype':'enter',
'simple_means_flag': '3',
'sign':sign,
'token':'8dc5d6c964650695eeaf54bbc7789c31',
'domain':'common'
}
response = requests.post(url,headers=headers,data=data)
print(response.json())
request("hello")
运行发现报错: 告诉我们JS文件里面少了一个叫 i 的参数,在function e? 里面看看哪里调用了这个i 调试发现这个i是不变的,那我们就在我们创建的JS文件里添加上去 在次运行还是报错,告诉我们少了一个n的参数 那我们还是跟上面一样在function e? 里面找哪里调用了n 把复制出来的代码放在JS文件function e? 函数的上面: 重新运行发现就能拿到结果了。 爬虫代码上面已经给出,这是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)
}
|