爬虫-有道翻译

import requests,time,hashlib

def getMd5(value):
    md5 = hashlib.md5()
    md5.update(value.encode())
    return md5.hexdigest()

def request_html(url,word):
    salt_p = time.time()
    salt = str(int(salt_p*10000))
    ts = str(int(salt_p*1000))
    value = "fanyideskweb" + word + salt + "n%A-rKaT5fb[Gy?;N5@Tj"
    sign = getMd5(value)
    data = {
        'i': word,
        'from': 'AUTO',
        'to': 'AUTO',
        'smartresult': 'dict',
        'client': 'fanyideskweb',
        'salt': salt,
        'sign': sign,
        'ts': ts,
        'bv': '8d08292ae0a71f68fb3e1c4056f90492',
        'octype': 'json',
        'version': '2.1',
        'keyfrom': 'fanyi.web',
        'action': 'FY_BY_REALTlME'
    }
    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': str(233+len(word)),
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie': 'OUTFOX_SEARCH_USER_ID=1181288453@10.169.0.84; JSESSIONID=aaaJB1jaPZXntbEqRRB1w; OUTFOX_SEARCH_USER_ID_NCOO=1493121319.1910746; ___rl__test__cookies=1569203481159',
        'Host': 'fanyi.youdao.com',
        'Origin': 'http://fanyi.youdao.com',
        'Referer': 'http://fanyi.youdao.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest'
    }
    response = requests.post(url, headers=headers, data=data).json()
    return response
def parse_response(response):
    translateResult = response['smartResult']['entries']
    for i in translateResult:
        print(i)

if __name__ == '__main__':
    while True:
        url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
        word = input('请输入单词:')
        try:
            response = request_html(url, word)
            # print(response)
            parse_response(response)
        except:
            print('没有此单词')


上一篇:python给字段名和值都加上引号


下一篇:网上翻译嫌麻烦?Python破解有道JS加密,让你随时能翻译