《封号码罗》python爬虫之某度翻译网站js逆向(十五)

.py
import requests
import execjs


def get_sign(word):
    with open("baidufanyi.js", "r", encoding="utf-8") as f:
        jscode = f.read()
    # https://blog.csdn.net/aobian2884/article/details/101404508/ 参考 此处jsdom安装失败
    etx = execjs.compile(jscode, cwd=r"D:\nvm\nodejs\node_modules")
    sign = etx.call("e", word)
    return sign


def request(word):
    sign = get_sign(word=word)

    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/89.0.4389.82 Safari/537.36",
        "Cookie": 'BIDUPSID=FCA50E388F350B269B3A9569B6315917; PSTM=1603805154; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; SOUND_SPD_SWITCH=1; HISTORY_SWITCH=1; SOUND_PREFER_SWITCH=1; BAIDUID=CA3A89DA7EBDF8FE124E7481C1CF3E3E:FG=1; __yjs_duid=1_0fa310593a7581945ac697fd344bcc701614170238963; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BAIDUID_BFESS=B88713A0A40FE577C4275CB3134C3086": "FG=1; delPer=0; PSINO=5; H_PS_PSSID=33636_33273_31660_33595_33570_33392_33601_33714_33268; BA_HECTOR=a5al0k0hal010ga1cm1g56ph20r; BCLID=7727619048270262268; BDSFRCVID=xQLOJexroG3VC5Qe_UPCqhta3uweG7bTDYLEOwXPsp3LGJLVJeC6EG0Pts1-dEu-EHtdogKK0gOTH6KF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=tR3aQ5rtKRTffjrnhPF32h_TXP6-hnjy3b7p5K5l5x7lVhOlMbOHDnLWbttf5q3RymJ42-39LPO2hpRjyxv4y4Ldj4oxJpOJ-bCL0p5aHl51fbbvbURvD--g3-AqBM5dtjTO2bc_5KnlfMQ_bf--QfbQ0hOhqP-jBRIE3-oJqC8-hC0m3j; BCLID_BFESS=7727619048270262268; BDSFRCVID_BFESS=xQLOJexroG3VC5Qe_UPCqhta3uweG7bTDYLEOwXPsp3LGJLVJeC6EG0Pts1-dEu-EHtdogKK0gOTH6KF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF_BFESS=tR3aQ5rtKRTffjrnhPF32h_TXP6-hnjy3b7p5K5l5x7lVhOlMbOHDnLWbttf5q3RymJ42-39LPO2hpRjyxv4y4Ldj4oxJpOJ-bCL0p5aHl51fbbvbURvD--g3-AqBM5dtjTO2bc_5KnlfMQ_bf--QfbQ0hOhqP-jBRIE3-oJqC8-hC0m3j; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1614787306,1614787327,1615035299,1616078375; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1616078375; ab_sr=1.0.0_NjcyNWMyYzAyMzFjZGNlMjVmZWIyNzA4NDExN2EzODMzNDJlN2UyMGJmODc2ODcwNTJlZmY3MTFkMjYwM2Y3OTZiOWZjZjc2N2I4YWZkMWUzZDg1NWIwZTVlZThiNTMz; __yjsv5_shitong=1.0_7_ce4125c17d0bc8d68307e2df69de79972985_300_1616078373757_49.64.60.81_9a3c0a18',
    }

    data = {
        "from": "en",
        "to": "zh",
        "query": word,
        "transtype": "realtime",
        "simple_means_flag": 3,
        # "sign": "871501.634748",
        "sign": sign,
        "token": "2a47aa16ad5a7d4e5056360f8abd7c66",
        "domain": "common",
    }
    response = requests.post(url=url, headers=headers, data=data)
    print(response.json())


if __name__ == '__main__':
    request("dog")

.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)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值