爬虫 pc版百度翻译接口 post方式
分析过程,略……,有时间再补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']['dst'])
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 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 && f.push.apply(f, a(e.split(""))),
C !== h - 1 && f.push(o);
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, s = Number(d) || 0, S = [], c = 0, v = 0; v < r.length; v++) {
var A = r.charCodeAt(v);
128 > A ? S = A: (2048 > A ? S = 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 = A >> 18 | 240, S = A >> 12 & 63 | 128) : S = A >> 12 | 224, S = A >> 6 & 63 | 128), S = 63 & A | 128)
}
for (var p = m,
F = "+-a^+6",
D = "+-3^+b+-f",
b = 0; b < S.length; b++) p += S,
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文件同目录
给楼主赞一个! 就一个翻译api,
爬啥了.. 来看看楼主 Vvvvvoid 发表于 2018-7-25 16:20
就一个翻译api,
爬啥了..
请大神分析一下这个网页接口(即api),让我学习学习,谢谢! 感谢分享! 谢谢老哥分享,sign一直搞不出来 略加修改,很有用的。
感谢分享! 感谢分享 写的贼6 支持一波 点赞{:1_921:}
页:
[1]
2