想要充实自己 发表于 2020-8-21 09:25:01

又又又出错了,爬取有道翻译出错了,求解

我写的代码如下:
import urllib.request
import urllib.parse

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

data = {}

data['i'] = 'hello world'
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '15979710202036'
data['sign'] = '98b135fb6a908cd47e75aef09b704a9f'
data['lts'] = '1597971020203'
data['bv'] = '94d04da9bee8870ad9ad8714b54f2bea'
data['doctype'] ='json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_REALTlME'

data = urllib.parse.urlencode(data).decode('utf-8')

response = urllib.request.urlopen(url,data)

html = response.read().decode('utf-8')

print(html)

跟甲鱼老师一步步写的,其中Form Data不一样,于是我就按网页上的写了,但是这个代码点F5运行的话没有任何反应,也没有报错,这是为什么呢??
我看视频课程上甲鱼老师的可以正常运行啊

Twilight6 发表于 2020-8-21 09:39:47

本帖最后由 Twilight6 于 2020-8-21 09:40 编辑


有道反爬,要把 url 中的 _o 去掉

而且你代码中的 urllib.parse.urlencode(data).decode('utf-8')改成 urllib.parse.urlencode(data).encode('utf-8'),就行了

import urllib.request
import urllib.parse

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

data = {}

data['i'] = 'hello world'
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '15979710202036'
data['sign'] = '98b135fb6a908cd47e75aef09b704a9f'
data['lts'] = '1597971020203'
data['bv'] = '94d04da9bee8870ad9ad8714b54f2bea'
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)

运行结果:
                        {"type":"EN2ZH_CN","errorCode":0,"elapsedTime":1,"translateResult":[[{"src":"hello world","tgt":"你好,世界"}]]}

mzx666 发表于 2020-8-21 09:54:48

用requests多香~

一抹心尘 发表于 2020-10-29 15:58:11

尽信书则不如无书,小甲鱼的书是几年前的了,代码要不断更新才能适应。
你的代码倒数第4行,那里有错误,我帮你改了一下,decode打错啦,应该是encode。

我改了一下,现在应该可以了,你试一下。
import urllib.request
import urllib.parse

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

data = {}

data['i'] = 'hello world'
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '15979710202036'
data['sign'] = '98b135fb6a908cd47e75aef09b704a9f'
data['lts'] = '1597971020203'
data['bv'] = '94d04da9bee8870ad9ad8714b54f2bea'
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)

小伤口 发表于 2020-12-13 21:12:34

俺想请教一下你这个有道是在哪里找到的呀,我找的翻译不了呀

你的鱼 发表于 2021-7-23 22:39:06

太强了
页: [1]
查看完整版本: 又又又出错了,爬取有道翻译出错了,求解