标签:attr 类型 auto col type ec2 client imp json
import urllib.request
import urllib.parse
url = 【‘http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule‘】
data = {}
data[‘i‘] =‘fish‘
data[‘from‘]= ‘AUTO‘
data[‘to‘]= ‘AUTO‘
data[‘smartresult‘]= ‘dict‘
data[‘client‘]= ‘fanyideskweb‘
data[‘salt‘]=‘15828580361346‘
data[‘sign‘]=‘18c621974c28e73211635b6bdb3a7d73‘
data[‘ts‘]=‘1582858036134‘
data[‘bv‘]=‘bc250de095a39eeec212da07435b6924‘
data[‘doctype‘]=‘ json‘
data[‘version‘]=‘2.1‘
data[‘keyfrom‘]=‘fanyi.web‘
data[‘action‘]=‘FY_BY_REALTlME‘
data = urllib.parse.urlencode(data).encode(‘UTF-8‘)
response = urllib.request.urlopen(url,data)
html = response.read().decode(‘UTF-8‘)
print(html)
上面代码会报错,因为 urllib.request.urlopen()传参的是str类型,不是list,
所以url=‘http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule‘,
然后data[‘doctype‘]=‘ json‘这些字典里不能有空格,要不然得到的是整个网页
最终得到的应该是{"type":"EN2ZH_CN","errorCode":0,"elapsedTime":12,"translateResult":[[{"src":"fish","tgt":"鱼"}]]}
其中"src":"fish","tgt":"鱼",这里便是翻译的内容
AttributeError: 'list' object has no attribute 'data'智障错误
标签:attr 类型 auto col type ec2 client imp json
原文地址:https://www.cnblogs.com/mmjjww/p/12376657.html