python-爬取有道翻译

用urllib对有道翻译进行爬取

import urllib.request
import urllib.parse
import json

contant = input('请输入需要翻译的内容:')
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data={}
data['i'] = contant 
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '1542512180461'
data['sign'] = 'df8d239f7f95a75c9aa1aa1cc86d7e2e'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data[']action'] = 'FY_BY_REALTIME'
data['typoResult'] = 'flase'
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('翻译结果:' + target["translateResult"][0][0]["tgt"])

在爬取过程中报错{“errorCode”:50},上网查询后发现只要将

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

中的translate_o后面的_o去掉即可正常运行。
但原因还是不清楚,希望有大佬解读一下。

你可能感兴趣的:(机器学习-python)