How to call Python proper way translation API (without having to break the timestamp)

Before I always use MATLAB to implement various API calls, but I know MATLAB flexibility in this area it is slightly worse than Python, so this time wanted to turn into a variety of use API Python to achieve.
Here Insert Picture Description
This is the official explanation, we need to replace only the translated text back, but if we need the English translation you need to get the word coding, if English is not control. Format may be returned xml, may also be json, here we have chosen is json, return results can be processed directly by json package.

import urllib.request
import json
import urllib.parse

a = input('输入需要翻译的词语:')
data = {}
data['q' ] = a
data['only'] = 'translate'
data = urllib.parse.urlencode(data).encode('utf-8') # 这里需要编码一下,但是这个函数输入参数只能是字典
website='http://fanyi.youdao.com/openapi.do?keyfrom=cxvsdffd33&key=1310976914&type=data&doctype=json&version=1.1'
response = urllib.request.urlopen(website,data)
html = response.read() # 返回json格式数据
html = json.loads(html)
html = html['translation'] # 取出translation里面的内容
print(html)

Here Insert Picture Description

Published 58 original articles · won praise 69 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_43157190/article/details/104792937