0x01利用python调用有道翻译接口

0x01 利用python调用有道翻译接口

import urllib.request
import urllib.parse
import json

el = input("请输入要翻译的内容:")
#这是有道翻译页面的接口通过控制台可以拿到
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'

data = {}
data['i'] = el
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '1529484719582'
data['sign'] = 'e7e07caae822a4227be1bedc0d7aa76a'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CLICKBUTTION'
data['typoResult'] = 'false'

data = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.urlopen(url,data)
html = response.read().decode('utf-8')
target = json.loads(html)
print("翻译结果为: %s"% (target['translateResult'][0][0]['tgt']))

你可能感兴趣的:(0x01利用python调用有道翻译接口)