【python】20行代码实现有道api接口调用


1. 目标站点

http://fanyi.youdao.com/

2. 完整代码

import requests

def main(query):
    url = 'http://fanyi.youdao.com/translate'
    data = {
        "i": query,  # 待翻译的字符串
        "from": "AUTO",
        "to": "AUTO",
        "smartresult": "dict",
        "client": "fanyideskweb",
        "salt": "16081210430989",
        "doctype": "json",
        "version": "2.1",
        "keyfrom": "fanyi.web",
        "action": "FY_BY_CLICKBUTTION"
    }
    res = requests.post(url, data=data).json()
    print(res['translateResult'][0][0]['tgt'])  # 打印翻译后的结果

main('你好') # 输出: hello

3. 测试样例

3.1. 测试样例Ⅰ(汉译英)

样例输入世界

import requests

def main(query):
    url = 'http://fanyi.youdao.com/translate'
    data = {
        "i": query,  # 待翻译的字符串
        "from": "AUTO",
        "to": "AUTO",
        "smartresult": "dict",
        "client": "fanyideskweb",
        "salt": "16081210430989",
        "doctype": "json",
        "version": "2.1",
        "keyfrom": "fanyi.web",
        "action": "FY_BY_CLICKBUTTION"
    }
    res = requests.post(url, data=data).json()
    print(res['translateResult'][0][0]['tgt'])  # 打印翻译后的结果

main('世界')

样例输出The world

3.2. 测试样例Ⅱ(英译汉)

样例输入The world

import requests

def main(query):
    url = 'http://fanyi.youdao.com/translate'
    data = {
        "i": query,  # 待翻译的字符串
        "from": "AUTO",
        "to": "AUTO",
        "smartresult": "dict",
        "client": "fanyideskweb",
        "salt": "16081210430989",
        "doctype": "json",
        "version": "2.1",
        "keyfrom": "fanyi.web",
        "action": "FY_BY_CLICKBUTTION"
    }
    res = requests.post(url, data=data).json()
    print(res['translateResult'][0][0]['tgt'])  # 打印翻译后的结果

main('The world')

样例输出世界

4. 调用文档

4.1. 接口地址

http://fanyi.youdao.com/translate

4.2. 请求方法

post

4.3. 请求参数

参数类型描述
istr待翻译的字符串
fromstr源语言
tostr翻译后的语言
smartresultstr\
clientstr客户端
saltstr\
doctypestr返回文档类型
versionstr版本号
keyfromstr键来源
actionstr执行翻译的动作

4.4. 请求示例

import json, requests

def main(query):
    url = 'http://fanyi.youdao.com/translate'
    data = {
        "i": query,  # 待翻译的字符串
        "from": "AUTO",
        "to": "AUTO",
        "smartresult": "dict",
        "client": "fanyideskweb",
        "salt": "16081210430989",
        "doctype": "json",
        "version": "2.1",
        "keyfrom": "fanyi.web",
        "action": "FY_BY_CLICKBUTTION"
    }
    res = requests.post(url, data=data).json()
    print(json.dumps(res, indent=2, ensure_ascii=False))

main('hello')

4.5. 成功响应

{
  "type": "EN2ZH_CN",
  "errorCode": 0,
  "elapsedTime": 1,
  "translateResult": [
    [
      {
        "src": "hello",
        "tgt": "你好"
      }
    ]
  ]
}

5. 接口分析

1、进入有道翻译页面,F12打开开发者调试工具,选择Network-XHR
在这里插入图片描述

2、我们马上就发现了一个api接口:

http://fanyi.youdao.com/translate_o

3、然后我们看一下它的请求参数:
在这里插入图片描述

4、使用python在后端发送请求:

import requests
import json
url = 'http://fanyi.youdao.com/translate_o'
data = {
    "i": "你好",  # 待翻译的字符串
    "from": "AUTO",
    "to": "AUTO",
    "smartresult": "dict",
    "client": "fanyideskweb",
    "salt": "16081239145423",
    "sign": "d567c9205219fd9d3aa9c677d1535212",
    "lts": "1608123914542",
    "bv": "495f346d16cfce476d93dc879b9e4485",
    "doctype": "json",
    "version": "2.1",
    "keyfrom": "fanyi.web",
    "action": "FY_BY_REALTlME"
}
res = requests.post(url, data=data).json()
print(json.dumps(res, indent=2, ensure_ascii=False))

5、响应结果:

{
  "errorCode": 50
}

6、额……报错(⊙﹏⊙)。怎么办呢?/(ㄒoㄒ)/~~……别急接着往下看。


1、在github上又看到了一个api接口,就是把之前那个接口的_o去掉:

http://fanyi.youdao.com/translate

2、然后就请求成功了:

{
  "type": "ZH_CN2EN",
  "errorCode": 0,
  "elapsedTime": 0,
  "translateResult": [
    [
      {
        "src": "你好",
        "tgt": "hello"
      }
    ]
  ]
}

3、请求参数其实还可以做一些删减,signltsbv都不需要,可以删掉。
4、然后actionFY_BY_REALTlMEFY_BY_CLICKBUTTION中任意选择一个都行。
5、很显然,res['translateResult'][0][0]['tgt']中的字符串就是你好翻译后结果。
6、其实salt参数也没啥用,可以不要。

6. 相关推荐

【python】5行代码实现百度翻译api接口调用

posted @ 2020-12-16 21:24  XavierJ  阅读(339)  评论(0编辑  收藏  举报