【问题标题】:Beautiful Soup Select Vs Find_all data TypeBeautiful Soup Select Vs Find_all 数据类型
【发布时间】:2018-03-31 21:36:20
【问题描述】:

我是网络抓取的新手,似乎有两种方法可以收集我正在寻找的所有 html 数据。

option_1 = soup.find_all('div', class_='p')

option_2 = soup.select('div.p')

我看到 option_1 返回类 'bs4.element.ResultSet' 而 option_2 返回类 'list'

我仍然可以使用 for 循环遍历 option_1,那么两者有什么区别:

  1. 选择并查找_all
  2. 'list'和bs4.element.ResultSet

【问题讨论】:

标签: python beautifulsoup


【解决方案1】:

您应该找到第一个问题here 的答案(由 cmets 中的t-m-adam 链接)。

至于第二个问题,我们看一下源码:)

class ResultSet(list):
    """A ResultSet is just a list that keeps track of the SoupStrainer
    that created it."""
    def __init__(self, source, result=()):
        super(ResultSet, self).__init__(result)
        self.source = source

    def __getattr__(self, key):
        raise AttributeError(
            "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
        )

ResultSet 只是list 的子类,用于存储find_all() 方法的结果。

【讨论】:

    猜你喜欢
    • 2020-06-06
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多