【python】20行代码实现有道api接口调用
文章目录
1. 目标站点
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. 请求参数
参数 | 类型 | 描述 |
---|---|---|
i | str | 待翻译的字符串 |
from | str | 源语言 |
to | str | 翻译后的语言 |
smartresult | str | \ |
client | str | 客户端 |
salt | str | \ |
doctype | str | 返回文档类型 |
version | str | 版本号 |
keyfrom | str | 键来源 |
action | str | 执行翻译的动作 |
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、请求参数其实还可以做一些删减,sign
、lts
和bv
都不需要,可以删掉。
4、然后action
从FY_BY_REALTlME
或FY_BY_CLICKBUTTION
中任意选择一个都行。
5、很显然,res['translateResult'][0][0]['tgt']
中的字符串就是你好
翻译后结果。
6、其实salt
参数也没啥用,可以不要。