【问题标题】:Getting error AttributeError: ResultSet object has no attribute 'find_all'出现错误 AttributeError:ResultSet 对象没有属性“find_all”
【发布时间】:2021-07-22 21:29:55
【问题描述】:

您好,我正在尝试找出分页内容下的所有链接以及已提取的分页部分代码。但是当我试图捕获所有列表项时,出现以下错误:

AttributeError:ResultSet 对象没有属性“find_all”。您可能将元素列表视为单个元素。当您打算调用 find() 时,您是否调用了 find_all()?

import requests
from bs4 import BeautifulSoup

url = "https://scrapingclub.com/exercise/list_basic/"

response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')

pages = soup.find_all('ul', class_='pagination')
links = pages.find_all('a', class_='page-link')
print(links)

我不理解 AttributeError 一词:ResultSet 对象没有属性“find_all”。 任何人都可以检查一下我所缺少的内容。

【问题讨论】:

标签: beautifulsoup


【解决方案1】:

问题是您不能在第一次 .find_all() 调用返回的 ResultSet 上调用 .find_all().find()

这个例子将打印来自pagination的所有链接:

import requests
from bs4 import BeautifulSoup

url = "https://scrapingclub.com/exercise/list_basic/"

response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')

pages = soup.find('ul', class_='pagination')          # <-- .find() to return only one element

for link in pages.find_all('a', class_='page-link'):  # <-- find_all() to return list of elements
    print(link)

打印:

<a class="page-link" href="?page=2">2</a>
<a class="page-link" href="?page=3">3</a>
<a class="page-link" href="?page=4">4</a>
<a class="page-link" href="?page=5">5</a>
<a class="page-link" href="?page=6">6</a>
<a class="page-link" href="?page=7">7</a>
<a class="page-link" href="?page=2">Next</a>

【讨论】:

    猜你喜欢
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2018-05-02
    • 2014-06-04
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多