有道翻译参数demo

import hashlib
import time
import requests

def getMD5(v):
    md5 = hashlib.md5()
    md5.update(v.encode('utf-8'))
    sign = md5.hexdigest()
    return sign

def get_salt():
    salt = str(int(time.time()*1000))
    return salt

def get_sigh(e, salt):
    sign = 'fanyideskweb' + e + salt + 'mmbP%A-r6U3Nw(n]BjuEU'
    return getMD5(sign)


def youdao(key):
    # url从http://fanyi.youdao.com输入词汇右键检查得到
    url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
    salt = get_salt()
    # data从右键检查FormData得
    data1 = {"i": key, "from": "AUTO", "to": "AUTO", "smartresult": "dict", "client": "fanyideskweb",
    "salt": salt, "sign": get_sigh(key,salt), "ts": '1592289678640',
    "bv": "e2a78ed30c66e16a857c5b6486a1d326", "doctype": "json", "version": "2.1", "keyfrom": "fanyi.web",
    "action": "FY_BY_CLICKBUTTION"}
    print(data1)
    # headers从右键检查Request Headers得到
    headers = {
        'Cookie': 'OUTFOX_SEARCH_USER_ID = "[email protected]";',
        'Referer':'http://fanyi.youdao.com/',
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0',
        }
    req = requests.post(url=url, data=data1, headers=headers)
    print(req.text)


if __name__ == '__main__':
    youdao("hell")

你可能感兴趣的:(有道翻译参数demo)