Python破解有道翻译js加密反爬

必须传入以下参数 
i: 多么操蛋的 
from: AUTO
to: AUTO
smartresult: dict
client: fanyideskweb
salt: 15639358877919   #不断变化  盐值 
sign: a4c83eec2d54d4b4d783182da801d34b # 签名  不断变化   
ts: 1563935887791 #时间戳 不断变化
bv: 53539dde41bde18f4a71bb075fcf2e66
doctype: json
version: 2.1
keyfrom: fanyi.web
action: FY_BY_REALTlME

这些字段是怎么来的?

一般是js写的 抓取fanyi.min.js  采用格式化工具  
截取以下片段 

function(e, t) {
     var n = e("./jquery-1.7");
     e("./utils");
     e("./md5");
     var r = function(e) {
         var t = n.md5(navigator.appVersion),
         r = "" + (new Date).getTime(),
         i = r + parseInt(10 * Math.random(), 10);
         return {
             ts: r,
             bv: t,
             salt: i,
             sign: n.md5("fanyideskweb" + e + i + "n%A-rKaT5fb[Gy?;N5@Tj")
         }
     };
     
  
  
  bv 代表的当前有道翻译md5 编码 版本  一般是长时间不变的   
  ts 当前时间戳 
  salt 当前时间戳 + 1到10之间的随机数 
  sign  md5("fanyideskweb" + 要翻译的内容 + salt + "n%A-rKaT5fb[Gy?;N5@Tj")

反爬代码

from urllib import request, parse
import time
import random
import hashlib
import json


def get_ts(): #时间戳 乘以1000 取整  
    ts = str(int(1000 * time.time()))
    return ts


def get_salt(ts):
    salt = ts + str(random.randint(0, 10)) # 上面的方法返回值 加上1-10之间的随机数  
    return salt


def get_sign(words, salt):
    content = 'fanyideskweb' + words + salt + '1L5ja}w$puC.v_Kz3@yYn'
    sign = hashlib.md5(content.encode()).hexdigest()
    return sign


def translate(words, ts, salt, sign):
    url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"

    data = {
        "i": words,
        "from": "AUTO",
        "to": "AUTO",
        "smartresult": "dict",
        "client": "fanyideskweb",
        "salt": salt,
        "sign": sign,
        'ts': ts,
        'bv': 'bbb3ed55971873051bc2ff740579bb49',
        "doctype": "json",
        "version": "2.1",
        "keyfrom": "fanyi.web",
        "action": "FY_BY_REALTIME",
        "typoResult": "false"
    }

    data = parse.urlencode(data).encode()

    headers = {
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        # 'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Content-Length': len(data),
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie': '__guid=204659719.2422785200799945700.1554675512727.244; [email protected]; OUTFOX_SEARCH_USER_ID_NCOO=378292303.3354687; JSESSIONID=aaaLYwaICIOxi6ofRh8Nw; monitor_count=8; ___rl__test__cookies=1554693830913',
        'Host': 'fanyi.youdao.com',
        'Origin': 'http://fanyi.youdao.com',
        'Referer': 'http://fanyi.youdao.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest'
    }

    req = request.Request(url=url, data=data, headers=headers)
    rsp = request.urlopen(req)
    html = rsp.read().decode('utf-8')
    json_data = json.loads(html)
    print('翻译的结果为:' + json_data['translateResult'][0][0]['tgt'])


if __name__ == '__main__':
    words = input('请输入要翻译的内容:')
    ts = get_ts()
    salt = get_salt(ts)
    sign = get_sign(words, salt)
    translate(words, ts, salt, sign)

你可能感兴趣的:(Python破解有道翻译js加密反爬)