0
点赞
收藏
分享

微信扫一扫

百度翻译

代码敲到深夜 2021-09-28 阅读 89

闲来无事帮朋友扣的百度翻译

### file: js_code.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)
    }

### file: main.py
# encoding=utf-8
import execjs, requests, re
import io
import sys  # 由于print是函数,输出编码有问题,此为了调试过程中输出看
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')

query = 'name'  # 搜索词
with open('js_code.js', 'r', encoding='utf-8') as f:
    ctx = execjs.compile(f.read())
sign = ctx.call('e', query)

data = {
    "from": "en",
    "to": "zh",
    "query": query,  # query 即我们要翻译的的内容
    "transtype": "translang",
    "simple_means_flag": "3",
    "sign": "",  # sign 是变化的需要我们执行js代码得到
    "domain": "common"
}

data['sign'] = sign
headers = {
    'Connection': 'keep-alive',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache',
    'Accept': '*/*',
    'X-Requested-With': 'XMLHttpRequest',
    'sec-ch-ua-mobile': '?0',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Origin': 'https://fanyi.baidu.com',
    'Referer': 'https://fanyi.baidu.com/?aldtype=16047',
    'Accept-Language': 'zh-CN,zh;q=0.9',
}
url = "https://fanyi.baidu.com/v2transapi?from=en&to=zh"
sess = requests.session()
rr = sess.get('https://fanyi.baidu.com/?aldtype=16047', headers=headers)  # 首次获得BAIDUID
BAIDUID = rr.cookies['BAIDUID']
res = sess.get('https://fanyi.baidu.com/?aldtype=16047', headers=headers)  # 使用BAIDUID 获取到的html 有token值
token = re.findall("token: '(.*?)',", res.text)[0]
cookies = {'BAIDUID': BAIDUID,}
data['token'] = token  # BAIDUID和token 是绑定的 如果一直不变能用多久没有测试,可以写成缓存,不能用再更新
response = requests.post(url, headers=headers, data=data, cookies=cookies)
print(response.text)
举报

相关推荐

0 条评论