import urllib.request

import urllib.parse

import json

Content = input(” Please input content with translation: “)

url = ‘http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule’

data = {}

data[‘i’]=content

data[‘from’]=’AUTO’

data[‘to’]=’AUTO’

data[‘smartresult’]= ‘dict’

data[‘client’]= ‘fanyideskweb’

data[‘salt’]=’1535438497358′

data[‘sign’]= ‘f19e9cbeddb439ff1fcf17265010373e’

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)

target = target[‘translateResult’][0][0][‘tgt’]

print(target)

The errors are:



Please enter with translation: I

Traceback (most recent call last):

File “E:\python \54-1.py”, line 26, in <module>

target = target[‘translateResult’][0][0][‘tgt’]

KeyError: ‘translateResult’



Remove the _o after translate from the url obtained in the review element, and the error disappears

———————

Author: lgy54321

Source: CSDN

The original:Blog.csdn.net/lgy54321/ar…

Copyright notice: This article is the blogger’s original article, reprint please attach the blog link!

Learn more about Python at gzitcast