gpt4 book ai didi

python - 属性错误 : 'bytes' object has no attribute 'timeout'

转载 作者:太空狗 更新时间:2023-10-29 22:30:24 34 4
gpt4 key购买 nike

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 行(AttributeError: 'bytes' object has no attribute 'timeout')。我不完全理解错误,所以我正在寻找答案和对我做错了什么的解释。谢谢!

最佳答案

来自docs对于 urllib.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 没有收到字符串,它会假定它是一个 Request 对象。您正在传递一个字节串,这就是它失败的原因,例如:

>>> 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'

要解决这个问题,请使用适当的编解码器将字节串转换回 str :

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

或者一开始就不要使用字节串。

关于python - 属性错误 : 'bytes' object has no attribute 'timeout' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24049151/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com