1 #写一个翻译小工具 2 3 import urllib.request 4 import urllib.parse 5 import json 6 content = input('请输入要翻译的内容:') 7 8 url = 'http://fanyi.youdao.com/translate?smartresult= 9 dict&smartresult=rule&smartresult=ugc&sessionFrom=dict2.index' 10 11 data={ 12 'type':'AUTO', 13 'i':content, 14 'doctype':'json', 15 'xmlVersion':'1.8', 16 'keyfrom':'fanyi.web', 17 'ue':'UTF-8' 18 } 19 data = urllib.parse.urlencode(data).encode('utf-8') 20 response = urllib.request.urlopen(url,data) 21 html = response.read().decode('utf-8') 22 target = json.loads(html) 23 print('翻译结果是:%s'%(target['translateResult'][0][0]['tgt']))