【问题标题】:Can I scrape only elements that DON'T have a specific attribute?我可以只抓取没有特定属性的元素吗?
【发布时间】:2021-05-20 09:11:42
【问题描述】:

在 bs4 中,我想应用过滤器来抓取没有特定属性的元素。很难解释,所以考虑这个例子:

有 5 个“div”元素。其中 4 个具有“class”和“id”属性,第 5 个具有“class”、“id”和“style”属性。我只想抓取前 4 个。

我知道我可以使用soup.find_all('div', {'class': '<class-name>', 'id': '<id>'} 为我想要的属性应用过滤器,但我可以为我不想要的属性应用额外的过滤器吗?

请参阅下面的示例 HTML 代码:

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    使用 CSS Selector :not(),你可以得到这样的结果

    soup.select('div:not(div[style="display: none;"])')
    

    :not() 表示选择每个不是 div[style="display: none;"] 元素的元素

    【讨论】:

      猜你喜欢
      • 2021-01-07
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多