python爬取有道翻译出错 {‘errorcode’:50}

在用urllib对有道翻译进行爬取时,出现了错误。


import urllib.request

import urllib.parse
import json


content = input("Enter the words needs translated:")
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=ugc"
data = {}
data['i'] = content
data['from'] = 'AUTO'
data['to']='AUTO'
data['smartresult']='dict'
data['client']='fanyideskweb'
data['salt']='1513663501217'
data['sign']='6a516734349a812792c07491f639609e'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom']='fanyi.web'
data['action']='FY_BY_REALTIME'
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("result: %s" % (target['translateResult'][0][0]['tgt']))


将在审查元素中获得的url中translate后面的_o去掉,错误就消失了,可以正常爬取。不知道为什么


刚刚又试了一下,data除了doctype键和i键不能去掉,其余的即使删除了也能正常运行翻译。

你可能感兴趣的:(python爬取有道翻译出错 {‘errorcode’:50})