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”)。我不完全理解这个错误,所以我在寻找一个答案和解释我做错了什么。谢谢!
由于这是一个属性错误,您编写的某些代码或您使用的库中的某些代码试图访问传递它的对象的超时属性。在您的例子中,传递了一个bytes对象,这可能是您的问题。您可能在某个地方传递了错误的对象类型。如果您确定要传递的对象是正确的,请按照回溯查看调用timeout的确切位置,并检查是否可以知道它期望的对象。
此错误是由于您不能将bytestring用作url,请检查程序的编码
来自docs的
urllib.request.urlopen
:如果
urllib.request.urlopen
没有收到字符串,则假定它是一个请求对象。你正在通过一个bytestring,这就是它失败的原因,例如:要解决此问题,请使用适当的编解码器将bytestring解码回str:
或者首先不要使用bytestrings。
相关问题 更多 >
编程相关推荐