python 有道翻译 爬虫破解2020

# -*- coding:UTF-8 -*-
import hashlib
import random
import time
import requests as rq

header = {"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": "251",
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
          "Cookie": "[email protected]; JSESSIONID=aaam6bk-eDeyxg15AT_Qw; OUTFOX_SEARCH_USER_ID_NCOO=1805496899.1391485; ___rl__test__cookies=1557978014699",
          "Host": "fanyi.youdao.com",
          "Origin": "http://fanyi.youdao.com",
          "Referer": "http://fanyi.youdao.com/",
          "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36",
          "X-Requested-With": "XMLHttpRequest"}


def generate_salt_sign(translate):
    # var t = n.md5(navigator.appVersion)
    app_version = "5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
    bv = hashlib.md5(app_version.encode('utf-8')).hexdigest()
    print(bv)
    # r = "" + (new Date).getTime()
    ts = str(int(round(time.time(), 3) * 1000))
    # i = r + parseInt(10 * Math.random(), 10);
    salt = ts + str(random.randint(1, 10))

    # sign: n.md5("fanyideskweb" + e + i + "1L5ja}w$puC.v_Kz3@yYn")
    sign = hashlib.md5(("fanyideskweb" + translate + salt + "mmbP%A-r6U3Nw(n]BjuEU").encode('utf-8')).hexdigest()

    return salt, sign, ts, bv


##data={
##"i": translate,
##"from": "AUTO",
##"to": "AUTO",
##"smartresult": "dict",
##"client": "fanyideskweb",
##"doctype": "json",
##"version": "2.1",
##"keyfrom": "fanyi.web",
##"action": "FY_BY_REALTlME"
##
##    }
def params(info):
    data = {}
    translate = info
    client = 'fanyideskweb'
    data['i'] = translate
    data['from'] = 'AUTO'
    data['to'] = 'AUTO'
    data['smartresult'] = 'dict'
    data['client'] = client

    data['salt'], data['sign'], data['ts'], data['bv'] = generate_salt_sign(translate)
    data['doctype'] = 'json'
    data['version'] = '2.1'
    data['keyfrom'] = 'fanyi.web'
    data['action'] = 'FY_BY_REALTIME'
    data['typoResult'] = 'false'

    return data


def translate():
    info = input("请输入一个中文或者英文:")
    reponse = rq.post("http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule", headers=header,
                      data=params(info))

    jreponse = reponse.json()
    print(jreponse)
    print(jreponse['translateResult'][0][0]['tgt'])


while 1:
    translate()

 

你可能感兴趣的:(python,爬虫)