url = "https://www.cnn.com/"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
links = []
for link in soup(response).find_all("a", href=True):
links.append(link["href"])
for link in links:
print(links)
AttributeError:ResultSet 对象没有属性“find_all”。您可能将元素列表视为单个元素。当您打算调用 find() 时,您是否调用了 find_all() ?
我不太确定为什么会收到此错误,我正在尝试从该网站抓取所有 href/链接。
最佳答案
不需要调用soup(response)
,直接在soup soup
上调用find_all
即可。 Soup 已经有了第 5 行的响应信息,所以它是多余的。
# Replace this:
for link in soup(response).find_all("a", href=True):
# With this
for link in soup.find_all("a", href=True):
关于python - 链接抓取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74426805/