小白js逆向 百度翻译(终于搞定了)
本帖最后由 lihu5841314 于 2021-6-22 19:08 编辑import requests
import execjs
url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'
headers ={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36',
'Referer':'https://fanyi.baidu.com/',
"Cookie": "BAIDUID=C35C955E94E1B00742B2C97034951AF6:FG=1; __yjs_duid=1_83429efe2386c3724b6e20cf7ba33a851618653053806; BIDUPSID=C35C955E94E1B00742B2C97034951AF6; PSTM=1618828649; BDUSS=lmcVVidXlDfmlDS25IM3U5WHhKbldEemZ5b2lCa3JwY1RhbTAwWjlIOVYzYWhnRVFBQUFBJCQAAAAAAAAAAAEAAABAQuMDbGlodTU4NDEzMTQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFVQgWBVUIFgTW; BDUSS_BFESS=lmcVVidXlDfmlDS25IM3U5WHhKbldEemZ5b2lCa3JwY1RhbTAwWjlIOVYzYWhnRVFBQUFBJCQAAAAAAAAAAAEAAABAQuMDbGlodTU4NDEzMTQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFVQgWBVUIFgTW; BDSFRCVID_BFESS=H48OJeCmHlfkH-cexrtaU9RUjgKK0gOTHllnqwNqSD64Ka4VJeC6EG0Ptf8g0KubFTPRogKK0gOTH6KF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF_BFESS=tJkD_I_hJKt3fP36q6_a2-F_2xQ0etJXf2-JVp7F5lOVObRIMUAV-PLbKlOKL6cNQCQMahkM5h7xObR1hRQbXR_9QaDJB-_JQeQ-5KQN3KJmfbL9bT3v5tDz3b3N2-biWbRM2MbdJqvP_IoG2Mn8M4bb3qOpBtQmJeTxoUJ25DnJhhCGe4bK-TrXjHtHJM5; BAIDUID_BFESS=C35C955E94E1B00742B2C97034951AF6:FG=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1624358243; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1624358243; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; __yjs_st=2_NGVmYmE0NWY2ZjY4ZmJiY2I0MGFmN2ViMDRkNzVhNTQ2MjJjYTg5MmE4MTY4OTU2OTY4N2FjNTQ4MDcyYjU2NWM4YWJhODRkODA0YWE1ODg4ZTkyYjdmNGEzYmM2MDlkYjc0NDlmNjQ5ZWVjMDI0NjhhZDM1MzVjYzJhZWI2OWFlNmE1OGNjMjM4ZjQ3MjhiOTViOTc4YzQ3NWZjN2FlMmYzNmIwYzk2NmI0ZmY1M2I0NGI1ZmU1OTA4YjkxMTQ5YmFhZWZkNWI0Y2MzNGVkOGIyN2RiMjRkZWQ4ZWIxYTFmMjNjZTdhZWRiZTI1OTFiMWFjY2Q1MTY5ZWM3NGQxMl83XzU5M2E3OWQx; ab_sr=1.0.1_MTk1NzhlMzRhMjlhYzkxOTE5NDFlMTM2MDJkMWUyYWRiMDQ5MjAyYzk2Y2VmMTBlN2I4ZDVjMGRkYzVkNTJmM2M1MWY0N2VmNzMzZGE4OTRiODg3MjY4YmU4ZTZmM2FjZTQwM2RhYmU3YmRiMjcyODEyNWMxYzljNjgxNWJiZTVjMzQyMGE2ZWNmMzk4NTY5NGZmNDNmYzNkYjkzOTg2YjIxMTFiZTlhZWQ4ZDU3ZjQ1YTgzNDE4ZjA4MzI3ZjUy"
}
defget_sign(word):
#实例化一个node对象
node = execjs.get()
#将js文件编译
sign1=node.compile(open('baidu02.js',encoding='utf-8').read()) #在cmd里通过npm root -g 可以查看全局模块安装路径
print(sign1)
#执行js函数
# func = 'e("{0}")'.format(word)
sign = sign1.call("e",word)
print(sign)
return sign
word = str(input("请输入需要翻译的单词:"))
sign = get_sign(word)
data={
'from':'en',
'to':'zh',
'query':word,
'transtype':'enter',
'simple_means_flag':3,
'sign':sign,
'token':'dbaebd613702916d5d8d8d327ba6568f',
'domain':'common',
}
response =requests.post(url=url,headers=headers,data=data).json()
print(response)
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"; 现在百度翻译里面的i的不是前面那个数字了取n的数字试试对了
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 && f.push.apply(f, a(e.split(""))),
C !== h - 1 && f.push(o);
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 || "") || "";
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 = A : (2048 > A ? S = 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 = A >> 18 | 240,
S = A >> 12 & 63 | 128) : S = A >> 12 | 224,
S = A >> 6 & 63 | 128),
S = 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,
p = n(p, F);
return p = n(p, D),
p ^= s,
0 > p && (p = (2147483647 & p) + 2147483648),
p %= 1e6,
p.toString() + "." + (p ^ m)
好几个小时
页:
[1]