马钰乔布轩 发表于 2020-6-28 15:28:41

请大家帮我看一下哪里出错了,这个报错是什么意思,谢谢!

import urllib.request
import urllib.parse
import json

content = input("请输入需要翻译的内容:")

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

head = {}
head['user-Agent'] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"


data = {}
data['type'] = 'AUTO'
data['i'] = content
data['doctype'] = 'json'
data['xmlVersion'] = '1.6'
data['keyfrom'] = 'fanyi.web'
data['ue'] = 'UTF-8'
data['typoResult'] = 'true'
data = urllib.parse.urlencode(data).encode('utf-8')

response = urllib.request.urlopen(url, data,head)
html = response.read().decode('utf-8')
target = json.loads(html)

print("翻译结果:%s" % (target['translateResult']['tgt']))

之前运行的了,自从加了head就报错了

Twilight6 发表于 2020-6-28 15:34:21

本帖最后由 Twilight6 于 2020-6-28 15:42 编辑


'user-Agent'要大写。。。

而且 不能把 data 和 headers 放道 urlopen那要通过 Requests

现在有道反爬蛮麻烦滴,你可以试着运行下面代码,就会发现被他检测出来反爬了:


import urllib.request
import urllib.parse
import json

content = input("请输入需要翻译的内容:")

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

head = {}
head['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"


data = {}
data['type'] = 'AUTO'
data['i'] = content
data['doctype'] = 'json'
data['xmlVersion'] = '1.6'
data['keyfrom'] = 'fanyi.web'
data['ue'] = 'UTF-8'
data['typoResult'] = 'true'
data = urllib.parse.urlencode(data).encode('utf-8')
requese = urllib.request.Request(url,data=data,headers=head)
response = urllib.request.urlopen(requese)
html = response.read().decode('utf-8')
target = json.loads(html)

print("翻译结果:%s" % (target['translateResult']['tgt']))

建议你看下这个帖子,按照这个帖子来:

https://blog.csdn.net/sinat_38068807/article/details/89537251

tiger吴 发表于 2020-6-28 15:35:15

现在没人用urllib,都是用一个第三方库requests了,百度下下

yifenyu 发表于 2020-6-28 15:42:43

help(req.urlopen)
Help on function urlopen in module urllib.request:

urlopen(url, data=None, timeout=<object object at 0x0000029FF47C7D60>, *, cafile=None, capath=None, cadefault=False, context=None)

你去看下doc,urlopen 是没有headers 功能的 对应位置是timeout 所有要求一个float参数 报typeerror错

青出于蓝 发表于 2020-6-28 15:45:28

import urllib.request
import urllib.parse
import json

content = input("请输入需要翻译的内容:")

url = 'http://fanyi.youdao.com/translate'

headers = {}
headers['user-Agent'] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"


data = {}
data['type'] = 'AUTO'
data['i'] = content
data['doctype'] = 'json'
data['xmlVersion'] = '1.6'
data['keyfrom'] = 'fanyi.web'
data['ue'] = 'UTF-8'
data['typoResult'] = 'true'
data = urllib.parse.urlencode(data).encode('utf-8')

response1= urllib.request.Request(url, data,headers)
response = urllib.request.urlopen(response1)
html = response.read().decode('utf-8')

ta = json.loads(html)# json.loads()用于将str类型的数据转成dict。
#参考 Json模块dumps、loads、dump、load函数介绍
print(ta['translateResult']['tgt'])

改了一下,应该没问题
页: [1]
查看完整版本: 请大家帮我看一下哪里出错了,这个报错是什么意思,谢谢!