1. 程式人生 > >有道翻譯爬蟲(破解引數加密機制)

有道翻譯爬蟲(破解引數加密機制)

反爬機制:

salt與sign變化加密

解決:js生成

function(e, t) {

var n = e("./jquery-1.7");

e("./utils");

e("./md5");

var r = function(e) {

var t = "" + ((new Date).getTime() + parseInt(10 * Math.random(), 10));

return {

salt: t,

sign: n.md5("fanyideskweb" + e + t + "6x(ZHw]mwzX#u0V7@yfwK")

}

};

salt=t

sign=n.md5("fanyideskweb" + e + t + "6x(ZHw]mwzX#u0V7@yfwK")

t ="" + ((new Data).getTime() + parseInt(10 * Math.random(), 10));

當前時間(毫秒)+ 隨機0-10字串

程式碼實現:

import hashlib
import json

import requests
import time
import random

url="http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/53"}
def get_reqponse(keywords,salt,sign):
    data={
        'i': keywords,
        'from': 'AUTO',
        'to': 'AUTO',
        'smartresult': 'dict',
        'client': 'fanyideskweb',
        'salt': salt,
        'sign': sign,
        'doctype': 'json',
        'version': '2.1',
        'keyfrom': 'fanyi.web',
        'action': 'FY_BY_REALTIME',
        'typoResult': 'false'
    }
    req=requests.post(url,data,headers=headers)
    response=json.loads(req.text)
    result=response['translateResult'][0][0]['tgt']
    print(result)

if __name__ == '__main__':
    while True:
        keywords=input("輸入待翻譯的中文:")
        salt=str(int(time.time()*1000) + random.randint(1,10))
        sign=hashlib.md5(("fanyideskweb" + keywords + salt + "6x(ZHw]mwzX#u0V7@yfwK").encode('utf-8'))
        get_reqponse(keywords,salt,sign)