百度翻译
百度翻译js破解
加密参数为sign js加密
token非加密字段,可固定
cookie 为必须字段,建议手动复制或者使用selenium 自动获取
import execjs import requests class BaiDuTranslate: def __init__(self, query): self.query = query self.url = "https://fanyi.baidu.com/v2transapi" self.headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36", "Cookie": "BIDUPSID=5CCE06DF3D9FCC9FA59C5719CB65583A; PSTM=1597453573; REALTIME_TRANS_SWITCH=1; HISTORY_SWITCH=1; FANYI_WORD_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDUSS=2QxeVY1bWpyRkJlNFl2ZjdiY01LdU1wUGxGNkVFUENHVVM5MHN2YzBpVUlpbjFmRVFBQUFBJCQAAAAAAAAAAAEAAAAG53QxsOvSucz0tca~tMutAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAj9VV8I~VVfV; BDUSS_BFESS=2QxeVY1bWpyRkJlNFl2ZjdiY01LdU1wUGxGNkVFUENHVVM5MHN2YzBpVUlpbjFmRVFBQUFBJCQAAAAAAAAAAAEAAAAG53QxsOvSucz0tca~tMutAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAj9VV8I~VVfV; BAIDUID=5CCE06DF3D9FCC9F4FEAC8424E401FB1:SL=0:NR=50:FG=1; MCITY=-%3A; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; PSINO=2; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; H_PS_PSSID=32815_32617_1422_32789_7547_31253_32795_32705_32231_7517_7605_32115_31709; delPer=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1602292161; yjs_js_security_passport=2976f4c2b600074f3271bf3abb4b5e6e6302d05b_1602292161_js; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1602292564; __yjsv5_shitong=1.0_7_aca7c4b90712886873246cbe7eccb883e219_300_1602292564401_61.163.131.118_f7dddd41" } self.data = { "from": "zh", "to": "en", "query": query, # query 即我们要翻译的的内容 "transtype": "translang", "simple_means_flag": "3", "sign": '', # sign 是变化的需要我们执行js代码得到 "token": "f25854730ff3972f3b9d59fa895a4e0c" # token没有变化 } self.js_path = r'E:\code\spiders\aiohppt_study\baidu_js.js' self.get_response() def get_sign(self): with open(self.js_path, 'r', encoding='utf8')as f: ctx = execjs.compile(f.read()) sign = ctx.call('e', self.query) return sign def get_response(self): sign = self.get_sign() self.data['sign'] = sign response = requests.post(self.url, headers=self.headers, data=self.data).json() r = response['trans_result']['data'][0]['dst'] print(r) if __name__ == '__main__': query = '你好' BaiDuTranslate(query)
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 } var i = "320305.131321201" function e(r) { 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) }