python 第54课:爬虫小问题
本帖最后由 Peteryo01223 于 2021-5-6 14:32 编辑问题:我跟随FishC视频,在有道翻译上做练习,run 后不成功,提示为
{"errorCode":50}
为何?
我的尝试如下:
import urllib.request
import urllib.parse
url = 'https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
data = {}
data['i'] = 'I love fish'
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '16202807937423'
data['sign'] = 'bbabea51cd64429d807bf3a4b1799e6c'
data['lts'] = '1620280793742'
data['bv'] = '74d66c0748efe9ba6ee38293a33b65d3'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CLICKBUTTION'
data = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.urlopen(url, data)
html = response.read().decode('utf-8')
print(html)
由于视频拍摄于2014年,与2021年我电脑情况,肯定有所不同。其中,关键的 request URL 和 Form data,如下,见两个截图:
请高手指点迷津!
本帖最后由 Peteryo01223 于 2021-5-6 15:52 编辑
网上搜索了一下,我把 URL 中 的一个 _o给删除了。然后,鼓捣了一下,竟然成功了。如下。但不知道背后的原因是什么。
import urllib.request
import urllib.parse
import json
content = input('请输入需要翻译的内容:')
url = 'https://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data = {}
data['i'] = content
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '16202807937423'
data['sign'] = 'bbabea51cd64429d807bf3a4b1799e6c'
data['lts'] = '1620280793742'
data['bv'] = '74d66c0748efe9ba6ee38293a33b65d3'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CLICKBUTTION'
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('翻译结果:%s' % (target['translateResult']['tgt']))
页:
[1]