2023 百度翻译 爬虫 js逆向 代码

js代码:

javascript 复制代码
const jsdom = require("jsdom");
const {JSDOM} = jsdom;
const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
window = dom.window;
document = window.document;
XMLHttpRequest = window.XMLHttpRequest;

function n(t, e) {
    for (var n = 0; n < e.length - 2; n += 3) {
        var r = e.charAt(n + 2);
        r = "a" <= r ? r.charCodeAt(0) - 87 : Number(r),
            r = "+" === e.charAt(n + 1) ? t >>> r : t << r,
            t = "+" === e.charAt(n) ? t + r & 4294967295 : t ^ r
    }
    return t
}

function sign(t) {
    var r = "320305.131321201";
    var o, i = t.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
    if (null === i) {
        var a = t.length;
        a > 30 && (t = "".concat(t.substr(0, 10)).concat(t.substr(Math.floor(a / 2) - 5, 10)).concat(t.substr(-10, 10)))
    } else {
        for (var s = t.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), c = 0, u = s.length, l = []; c < u; c++)
            "" !== s[c] && l.push.apply(l, function (t) {
                if (Array.isArray(t))
                    return e(t)
            }(o = s[c].split("")) || function (t) {
                if ("undefined" != typeof Symbol && null != t[Symbol.iterator] || null != t["@@iterator"])
                    return Array.from(t)
            }(o) || function (t, n) {
                if (t) {
                    if ("string" == typeof t)
                        return e(t, n);
                    var r = Object.prototype.toString.call(t).slice(8, -1);
                    return "Object" === r && t.constructor && (r = t.constructor.name),
                        "Map" === r || "Set" === r ? Array.from(t) : "Arguments" === r || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r) ? e(t, n) : void 0
                }
            }(o) || function () {
                throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
            }()),
            c !== u - 1 && l.push(i[c]);
        var p = l.length;
        p > 30 && (t = l.slice(0, 10).join("") + l.slice(Math.floor(p / 2) - 5, Math.floor(p / 2) + 5).join("") + l.slice(-10).join(""))
    }
    for (var d = "".concat(String.fromCharCode(103)).concat(String.fromCharCode(116)).concat(String.fromCharCode(107)), h = (null !== r ? r : (r = window[d] || "") || "").split("."), f = Number(h[0]) || 0, m = Number(h[1]) || 0, g = [], y = 0, v = 0; v < t.length; v++) {
        var _ = t.charCodeAt(v);
        _ < 128 ? g[y++] = _ : (_ < 2048 ? g[y++] = _ >> 6 | 192 : (55296 == (64512 & _) && v + 1 < t.length && 56320 == (64512 & t.charCodeAt(v + 1)) ? (_ = 65536 + ((1023 & _) << 10) + (1023 & t.charCodeAt(++v)),
            g[y++] = _ >> 18 | 240,
            g[y++] = _ >> 12 & 63 | 128) : g[y++] = _ >> 12 | 224,
            g[y++] = _ >> 6 & 63 | 128),
            g[y++] = 63 & _ | 128)
    }
    for (var b = f, w = "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(97)) + "".concat(String.fromCharCode(94)).concat(String.fromCharCode(43)).concat(String.fromCharCode(54)), k = "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(51)) + "".concat(String.fromCharCode(94)).concat(String.fromCharCode(43)).concat(String.fromCharCode(98)) + "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(102)), x = 0; x < g.length; x++)
        b = n(b += g[x], w);
    return b = n(b, k),
    (b ^= m) < 0 && (b = 2147483648 + (2147483647 & b)),
        "".concat((b %= 1e6).toString(), ".").concat(b ^ f)
}

function k(t) {
    var e = new RegExp("(^|&)" + t + "=([^&]*)(&|$)", "i")
        , n = window.location.search.substring(1).match(e);
    return null != n ? n[2] : null
}

console.log(window.location.search)
console.log(sign("aldtype"))

python代码

python 复制代码
import time
import execjs
import requests
from jsonpath import jsonpath

print("=" * 12)
print("百度翻译v1.0")
print("=" * 12)
while 1:
    str1 = input("请输入待翻译的内容 ==> ")
    url = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en'

    headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36',
        'referer': 'https://fanyi.baidu.com/translate?aldtype=16047&query=5&keyfrom=baidu&smartresult=dict&lang=auto2zh',
        'origin': 'https://fanyi.baidu.com',
        'cookie': 'BIDUPSID=47FEDF343AD6E5A34873E9967C34255F; PSTM=1683600014; BAIDUID=47FEDF343AD6E5A30BD8ECAEAD0226F7:FG=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; MCITY=-75%3A; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=36548_39226_39222_39193_39037_39199_26350_39138_39224_39137_39100; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1691492113,1691650752,1692253546,1692756754; BAIDUID_BFESS=47FEDF343AD6E5A30BD8ECAEAD0226F7:FG=1; BA_HECTOR=2ka024200g21840l8g80a48o1ieat8h1p; ZFY=3KeAbEqXQXnaHm:BmuP0PmpK:BQFBIvQ6Ui76ogvGZ2BM:C; delPer=0; PSINO=7; BDSFRCVID=BItOJexroG0ZmSbfTqkcT9G5OFweG7bTDYrEOwXPsp3LGJLVFakFEG0Pts1-dEu-S2OOogKKXgOTHw0F_2uxOjjg8UtVJeC6EG0Ptf8g0M5; BDSFRCVID_BFESS=BItOJexroG0ZmSbfTqkcT9G5OFweG7bTDYrEOwXPsp3LGJLVFakFEG0Pts1-dEu-S2OOogKKXgOTHw0F_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=tRAOoC_-tDvDqTrP-trf5DCShUFsJ58JB2Q-XPoO3KJADfOPblOtyn07etttWM5f5mkf3fbgy4op8P3y0bb2DUA1y4vp0toW3eTxoUJ2-KDVeh5Gqq-KXU4ebPRiXPb9QgbfopQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0hD89DjKKD6PVKgTa54cbb4o2WbCQ0qTM8pcN2b5oQT8lhJbaKq3D2K7nVl0aBhQbeq06-lOUWJDkXpJvQnJjt2JxaqRC3JjOsl5jDh3MKToDb-oteltHB2Oy0hvcyn3cShn6DMjrDRLbXU6BK5vPbNcZ0l8K3l02V-bIe-t2XjQh-p52f6FHfRvP; H_BDCLCKID_SF_BFESS=tRAOoC_-tDvDqTrP-trf5DCShUFsJ58JB2Q-XPoO3KJADfOPblOtyn07etttWM5f5mkf3fbgy4op8P3y0bb2DUA1y4vp0toW3eTxoUJ2-KDVeh5Gqq-KXU4ebPRiXPb9QgbfopQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0hD89DjKKD6PVKgTa54cbb4o2WbCQ0qTM8pcN2b5oQT8lhJbaKq3D2K7nVl0aBhQbeq06-lOUWJDkXpJvQnJjt2JxaqRC3JjOsl5jDh3MKToDb-oteltHB2Oy0hvcyn3cShn6DMjrDRLbXU6BK5vPbNcZ0l8K3l02V-bIe-t2XjQh-p52f6FHfRvP; BCLID=11529063074536128866; BCLID_BFESS=11529063074536128866; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1692762069; ab_sr=1.0.1_MDQ4ZjcyMDJlOTFmOGM0YzI1ZWUyMGM3ZTU4YmMwMjJhNWIwMmU3ODk3Zjg2ODM0YjdhMWIzZWQzZTFhYjY0YzY1NWVhNjY1ODYzNmIyMWJlYWViYWY5NDk2ZmQ0N2Y3ZmFhYmIzMTE2ODZkODQ1YmFlYjRhNjNkZTBkODFmYjIxMDA3NjZiOWU3NjI0M2M2YjhlYTAyNDlmMjMwOGNkZg=='
    }
    with open("百度翻译/11.js", 'r') as f:
        js_code = f.read()
    js = execjs.compile(js_code)
    result = js.call("sign", str1)

    data = {
        'from': 'auto',
        'to': 'auto',
        'query': str1,
        'transtype': 'realtime',
        'simple_means_flag': '3',
        'sign': str(result),
        'token': '0796ba47cb7ce221fa4b2f15c86cbcbe',
        'domain': 'common',
        "ts": int(time.time() * 1000)
    }

    response = requests.post(url, headers=headers, data=data)
    print("翻译的结果 ==> ", jsonpath(response.json(), '$..dst')[0], sep='')
    print("")

运行结果:

相关推荐
FreakStudio1 小时前
全网最适合入门的面向对象编程教程:56 Python字符串与序列化-正则表达式和re模块应用
python·单片机·嵌入式·面向对象·电子diy
丶21361 小时前
【CUDA】【PyTorch】安装 PyTorch 与 CUDA 11.7 的详细步骤
人工智能·pytorch·python
_.Switch2 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一路向前的月光2 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   2 小时前
vue-live2d看板娘集成方案设计使用教程
前端·javascript·vue.js·live2d
一个闪现必杀技2 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm
Fan_web2 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
小鹿( ﹡ˆoˆ﹡ )3 小时前
探索IP协议的神秘面纱:Python中的网络通信
python·tcp/ip·php
卷心菜小温3 小时前
【BUG】P-tuningv2微调ChatGLM2-6B时所踩的坑
python·深度学习·语言模型·nlp·bug
陈苏同学3 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm