有道词典没有成功暂存

2020-07-13  本文已影响0人  f5423d3134f0
import requests
import hashlib
import time
import random
import json


class YouDao(object):

    def __init__(self, word):
        self.url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
        self.word = word

        self.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":"276",
            "Content-Type":"application/x-www-form-urlencoded;charset=UTF-8",
            "Cookie":"OUTFOX_SEARCH_USER_ID=-882787379@10.108.160.18;JSESSIONID=aaaZLWFsuWKL47JKKranx; OUTFOX_SEARCH_USER_ID_NCOO=1473620521.1929467;DICT_UGC=be3af0da19b5c5e6aa4e17bd8d90b28a|;JSESSIONID=abccou3TFHtfV40lMUgnx; _ntes_nnid=c3f2c75abf8654976197a5b2f523d48a,1594619501505; ___rl__test__cookies=1594619524674",
            "Host":"fanyi.youdao.com",
            "Origin":"http://fanyi.youdao.com",
            "Referer":"http://fanyi.youdao.com/?keyfrom=dict2.top",
            "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36",
            "X-Requested-With":"XMLHttpRequest"

        }
        self.formdata = None

    def generate_formdata(self):
        '''
                   ts: "" + (new Date).getTime()
                   salt: ts + parseInt(10 * Math.random(), 10);
                   sign: n.md5("fanyideskweb" + e + i + "mmbP%A-r6U3Nw(n]BjuEU")
               '''
        ts = str(int(time.time() * 1000))
        salt = ts + str(random.randint(0, 9))
        tempstr = "fanyideskweb" + self.word + salt + "mmbP%A-r6U3Nw(n]BjuEU"
        md5 = hashlib.md5()
        md5.update(tempstr.encode())
        sign = md5.hexdigest()
        self.formdata = {
            "i": self.word,
            "from": "AUTO",
            "to": "AUTO",
            "smartresult": "dict",
            "client": "fanyideskweb",
            "salt": salt,
            "sign": sign,
            "ts": ts,
            "bv": "bae4fb40b166eb91e4fbab5cdd840d79",
            "doctype": "json",
            "version": "2.1",
            "keyfrom": "fanyi.web",
            "action": "FY_BY_CLICKBUTTION",
        }
        '''
        self.formdata = {
            "i":" 语文",
            "from":"AUTO",
            "to":"AUTO",
            "smartresult":"dict",
            "client":"fanyideskweb",
            "salt":"15946227455406",
            "sign":"5fd4912a911adbde3ec490996fb3c2e5",
            "ts":"1594622745540",
            "bv":"bae4fb40b166eb91e4fbab5cdd840d79",
            "doctype":" json",
            "version":" 2.1",
            "keyfrom":"fanyi.web",
            "action":"FY_BY_CLICKBUTTIO"
        }
          '''
    def get_data(self):

        response = requests.post(self.url,headers = self.headers,data=self.generate_formdata())
        return response.content
    def run(self):

        # url
        # headers
        # formdata
        self.generate_formdata()

        # 发送请求获取响应
        data = self.get_data()
        print(data)
        # 解析数据
if __name__ == '__main__':
    youdao = YouDao("语文")
    youdao.run()

上一篇 下一篇

猜你喜欢

热点阅读