是什么导致循环中的属性错误以及如何解决此问题

2024-05-04 06:45:20 发布

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

我得到了一个我能理解的错误,但用我目前发现的资源无法解决。我想做的是有一个简单的循环,从列表中获取一个url,请求其内容,打印输出,然后转到下一个url

f = open('urls.txt','r',encoding="utf8") #had to specify encoding because of error: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 3754: character maps to <undefined> 
content_urls = f.readlines()
f.close()

from urllib import request
from bs4 import BeautifulSoup
import time
for each in content_urls:
    time.sleep(1)
    scraped = request.urlopen(content_urls)
    soup = BeautifulSoup(scraped)
    print(soup)

前面提到的“列表”似乎正是问题所在: AttributeError:“list”对象没有属性“timeout”

第一次搜索让我想到: AttributeError: 'list' object has no attribute 'timeout' - Trying to process multiple URLs with BeautifulSoupPython list object has no attribute error'list' object has no attribute 'timeout' and only prints first item in the tableAttributeError: 'bytes' object has no attribute 'timeout'

但无论如何,我似乎无法实施这些解决方案。 是否需要将列表转换为字符串?我试过了,但似乎也没有成功

非常感谢您的帮助