import urllib.request
import urllib.parse
import json
content = input("请输入要翻译的内容:")
head = {}
head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0'
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
#data为一字典,有以下两种表示方法
'''
data = {}
data['type'] = "AUTO"
data['i'] = content
data['doctype'] = "json"
data['xmlVersion'] = "1.8"
data['keyfrom'] = "fanyi.web"
data['ue'] = "UTF-8"
data['action'] = "FY_BY_ENTER"
data['typoResult'] = "true"
'''
data = { 'type' :"AUTO",
'i' : content,
'doctype' :"json",
'xmlVersion' :"1.8",
'keyfrom' :"fanyi.web",
'ue': "UTF-8",
'action' :"FY_BY_ENTER",
'typoResult': "true"}
data = urllib.parse.urlencode(data).encode('utf-8')
req = urllib.request.Request(url,data,head)
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
target = json.loads(html)
print("翻译结果是:%s"%(target['translateResult'][0][0]['tgt']))
有道词典Python模拟翻译
最新推荐文章于 2022-01-07 11:48:25 发布