翻訳js暗号化解読
7090 ワード
実例説明:翻訳要求パラメータ(salt,sign,ts)はjs暗号化により爬虫類要求を行う際にまず暗号化されたパラメータを復号化し,js 2 py,hashlib暗号化,requests送信要求などを用いる.
import requests
import json
import js2py
import hashlib
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
'Referer': 'http://fanyi.youdao.com/',
'Cookie': '[email protected]; JSESSIONID=aaaHjtzn46bgpeP_sscGw; OUTFOX_SEARCH_USER_ID_NCOO=163114792.03463468; ___rl__test__cookies=1546265145496'
}
url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
# ts,salt : js2py
context = js2py.EvalJs()
js_str = """r = "" + (new Date).getTime()
, i = r + parseInt(10 * Math.random(), 10);"""
context.execute(js_str)
ts = context.r
salt = context.i
# sign : js python
e = input(' :')
sign = hashlib.md5(("fanyideskweb" + e + salt + "p09@Bn{h02_BIEe]$P^nG").encode('utf-8')).hexdigest()
data = {
'i': e,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': salt,
'sign': sign,
'ts': ts,
'bv': 'cf00c5d5529caf67ddf1aef1f05d88e4',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_CLICKBUTTION',
'typoResult': 'false'
}
resp = requests.post(url=url, headers=headers, data=data)
resp_dict = json.loads(resp.text)
ret = resp_dict['translateResult'][0][0]['tgt']
print(ret)