吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6876|回复: 10
收起左侧

[Python 转载] 爬虫 pc版百度翻译接口 post方式

[复制链接]
wtujoxk 发表于 2018-7-25 16:04
分析过程,略……,有时间再补


python代码:
[Python] 纯文本查看 复制代码
#!/usr/bin/env python
# -*- coding:utf-8 -*-

'''
爬虫之百度翻译
需要的库有 js2py, requests, re, json
'''

__author__ = 'YXQ'


import js2py
import requests
import json
import re

#百度翻译的主页
url_fanyi = 'http://fanyi.baidu.com'
#翻译时post的api
url_api = 'http://fanyi.baidu.com/v2transapi'
#设置 headers
headers = {
    'Cookie':'BIDUPSID=A360C8CE43082B9E2E23B5B111FC4363; PSTM=1485329732; to_lang_often=%5B%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%2C%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%5D; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=26524_1466_21124_18560_26350_22158; locale=zh; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1531663222,1531743493; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1531748592; from_lang_often=%5B%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%2C%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%5D; BAIDUID=96A2302A5D66AFBE539FFC68E881260F:FG=1',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4882.400 QQBrowser/9.7.13076.400',
}

#get百度翻译主页,是为了得到token
req_get = requests.get(url = url_fanyi, headers = headers)
token = re.search(r"token: '(.*?)',", req_get.text, re.S).group(1)

#需要翻译的内容,中英文都可以
translation_content = '我是一个中国人,我不为五斗米折腰'

#读入js代码
def get_js():
    with open('sign.js', 'r', encoding='utf-8') as f:
        return f.read()
#使用js2py在python中运行js代码并得到sign
run_js = js2py.EvalJs({})
run_js.execute(get_js())
sign = run_js.e(translation_content)

data = {
    'query':translation_content,
    'sign':sign,
    'token': token
}

req_post = requests.post(url = url_api, data = data, headers = headers)
result = json.loads(req_post.text)

print(result['trans_result']['data'][0]['dst'])




js代码:
[JavaScript] 纯文本查看 复制代码
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 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, i = null;
    u = null !== i ? i: (i = "320305.131321201" || "") || "";
    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 = "+-a^+6",
    D = "+-3^+b+-f",
    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)
}




将js代码保存为sign.js并放在python文件同目录

免费评分

参与人数 4吾爱币 +4 热心值 +3 收起 理由
水泥工艺学 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
maxiaohui + 1 + 1 感觉会有用的
52pojie_mxrain + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
隰则有泮 + 1 谢谢@Thanks!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

mark1001 发表于 2018-7-25 16:12
给楼主赞一个!
Vvvvvoid 发表于 2018-7-25 16:20
cyhcuichao 发表于 2018-7-25 22:07
 楼主| wtujoxk 发表于 2018-7-25 22:10
Vvvvvoid 发表于 2018-7-25 16:20
就一个翻译api,
爬啥了..

请大神分析一下这个网页接口(即api),让我学习学习,谢谢!
iwzw 发表于 2018-7-26 08:40
感谢分享!
水泥工艺学 发表于 2018-8-3 00:02
谢谢老哥分享,sign一直搞不出来
losingstars 发表于 2018-8-26 14:08
略加修改,很有用的。
huamu 发表于 2018-8-26 22:20

感谢分享!
小黑LLB 发表于 2019-2-11 16:03
感谢分享 写的贼6 支持一波 点赞
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 05:55

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表