python 有道翻译 爬虫破解

# -*- 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": "OUTFOX_SEARCH_USER_ID=1007047183@10.169.0.83; 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).hexdigest()
    # 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+"1L5ja}w$puC.v_Kz3@yYn")).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=raw_input("请输入一个中文或者英文:")
    reponse=rq.post("http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule",headers=header,data=params(info))

    jreponse=reponse.json()
    print jreponse['translateResult'][0][0]['tgt']
while 1:
    translate()

 

发布了20 篇原创文章 · 获赞 4 · 访问量 8043
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 终极编程指南 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览