AttributeError:“bytes”对象没有“timeout”属性

2024-05-14 04:32:20 发布

您现在位置:Python中文网/ 问答频道 /正文

import re, urllib.request

textfile = open('depth_1.txt','wt')
print('enter the url you would like to crawl')
print('Usage - "http://phocks.org/stumble/creepy/" <-- with the double quotes')
my_url = input()
for i in re.findall(b'''href=["'](.[^"']+)["']''', urllib.request.urlopen(my_url).read(), re.I):
    print(i)
    for ee in re.findall(b'''href=["'](.[^"']+)["']''', urllib.request.urlopen(i).read(), re.I): #this is line 20!
        print(ee)
        textfile.write(ee+'\n')
textfile.close()

在四处寻找我的问题的解决办法后,我找不到解决办法。错误出现在第20行(attribute error:“bytes”对象没有属性“timeout”)。我不完全理解这个错误,所以我在寻找一个答案和解释我做错了什么。谢谢!


Tags: theinreurlforreadrequestmy
3条回答

由于这是一个属性错误,您编写的某些代码或您使用的库中的某些代码试图访问传递它的对象的超时属性。在您的例子中,传递了一个bytes对象,这可能是您的问题。您可能在某个地方传递了错误的对象类型。如果您确定要传递的对象是正确的,请按照回溯查看调用timeout的确切位置,并检查是否可以知道它期望的对象。

此错误是由于您不能将bytestring用作url,请检查程序的编码

来自docsurllib.request.urlopen

urllib.request.urlopen(url[, data][, timeout])

    Open the URL url, which can be either a string or a Request object.

如果urllib.request.urlopen没有收到字符串,则假定它是一个请求对象。你正在通过一个bytestring,这就是它失败的原因,例如:

>>> a = urllib.request.urlopen('http://www.google.com').read() # success
>>> a = urllib.request.urlopen(b'http://www.google.com').read() # throws same error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 153, in urlopen
    return opener.open(url, data, timeout)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 446, in open
    req.timeout = timeout
AttributeError: 'bytes' object has no attribute 'timeout'

要解决此问题,请使用适当的编解码器将bytestring解码回str:

>>> a = urllib.request.urlopen(b'http://www.google.com'.decode('ASCII')).read()

或者首先不要使用bytestrings。

相关问题 更多 >