import requests
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
# 反爬
header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
}
def youdao(s):
    dat = {
        "i": s,  # 需要输入翻译的内容
        "from": "AUTO",
        "to": "AUTO",
        "smartresult": "dict",
        "client": "fanyideskweb",
        "doctype": "json",
        "version": "2.1",
        "keyfrom": "fanyi.web",
        "action": "FY_BY_CLICKBUTTION"
    }
    # 把结果以json的形式输出
    resp = requests.post(url, headers=header, data=dat).json()
    return resp['translateResult'][0][0]['tgt']
print(youdao('cat'))

标签: 有道词典, python函数

添加新评论