调用百度翻译api实现翻译功能

1.查看翻译所需的参数

对比几个可以发现参数中只有query,sign,token和ts值不一样

2.获取所需的参数

token与ts的值可以直接在源代码获取 

sessions = requests.Session()
sessions.headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 "
                  "Safari/537.36",
}

def translate(r):
    index = 0
    token = ""
    ts = ""
    gtk = ""
    while index < 3:
        index += 1
        token_resp = sessions.get("https://fanyi.baidu.com/")
        token = re.findall("token: '(.*?)'", token_resp.text)[0]
        ts = re.findall("systime: '(.*?)'", token_resp.text)[0]
        gtk = re.findall('window.gtk = "(.*?)"', token_resp.text)[0]
        if token and ts:
            break
    return token, ts, gtk

3.获取sign

sign的值比较难以获取

 

点击send可以跳转源代码 然后通过调试找到生成sign的源代码
调试到此处发现sign


最终找到生成sign函数

4.编写js文件

var r = null;

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,gtk) {
    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 = gtk || "") || "").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)
}

 5.编写翻译代码

先安装Pyexecjs库

pip install PyExecJs
import re
import requests
import execjs

sessions = requests.Session()
sessions.headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 "
                  "Safari/537.36",
}

def translate(r):
    index = 0
    token = ""
    ts = ""
    gtk = ""
    while index < 3:
        index += 1
        token_resp = sessions.get("https://fanyi.baidu.com/")
        token = re.findall("token: '(.*?)'", token_resp.text)[0]
        ts = re.findall("systime: '(.*?)'", token_resp.text)[0]
        gtk = re.findall('window.gtk = "(.*?)"', token_resp.text)[0]
        if token and ts:
            break
    return token, ts, gtk


def sign():
# 这里填写刚刚保存的js文件名称
    with open("sign.js", "r") as f:
        read = f.read()
        return execjs.compile(read)


if __name__ == '__main__':
    trans = input("请输入要翻译内容:")
    sign1 = sign()
    token, ts, gtk = translate(trans)
    call = sign1.call("sign", trans, gtk)
    form_data = {
        "from": "zh",
        "to": "en",
        "query": trans,
        "simple_means_flag": "3",
        "sign": call,
        "token": token,
        "domain": "common",
        "ts": ts
    }
    post = sessions.post('https://fanyi.baidu.com/v2transapi?from=zh&to=en', data=form_data)
    print(re.findall('"dst":"(.*?)"', post.text)[0])

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值