有道翻译接口获取

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有道翻译接口获取相关的知识,希望对你有一定的参考价值。

# coding:utf-8
import requests
import hashlib
import time
import json


def interpret(contents):
    url = http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule

    tt = int(time.time() * 1000)
    text = contents
    sig = "fanyideskweb" + text + str(tt) + "sr_3(QOHT)L2dx#[email protected]"
    sign = hashlib.md5(sig.encode(utf-8)).hexdigest()

    data = {
        "i": text,
        "from": "AUTO",
        "to": "AUTO",
        "smartresult": "dict",
        "client": "fanyideskweb",
        "salt": tt,
        "sign": sign,
        "doctype": "json",
        "version": "2.1",
        "keyfrom": "fanyi.web",
        "action": "FY_BY_REALTIME",
        "typoResult": "false",
    }

    headers = {
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Content-Length": "223",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "Cookie": "_ntes_nnid=4a572c8c5ac9526fdf4ba1240569aacd,1541596325684; OUTFOX_SEARCH_USER_ID_NCOO=1527476746.7479236; [email protected]; JSESSIONID=aaaMfSGQfy9VJhrvvGBCw; ___rl__test__cookies={}".format(
            tt),
        "Host": "fanyi.youdao.com",
        "Origin": "http://fanyi.youdao.com",
        "Pragma": "no-cache",
        "Referer": "http://fanyi.youdao.com/",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/70.0.3538.102 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest",
    }

    response = requests.post(url, headers=headers, data=data)

    content = response.content.decode(utf-8)

    return json.loads(content)[translateResult][0][0][tgt]


if __name__ == __main__:
    text = input(请你输入翻译内容:)

    print(interpret(text))


#
# ll =‘[1,2,"ll"]‘ # {"name":"1235"}
# ss = str(ll)
# print(json.loads(ss)[0])

 

以上是关于有道翻译接口获取的主要内容,如果未能解决你的问题,请参考以下文章

有道翻译js逆向更新了

Django怎么调用有道词典或金山词典api获取单词发音?

Python爬虫|有道翻译(简易版)

实战演示,调用有道翻译API接口进行英文转中文翻译实例训练

Python反编译调用有道翻译(附完整代码)

Android使用有道翻译API实如今线翻译功能