【问题标题】:Webscraping specific sections of page without 'class' or 'id' identifiers网页抓取没有“类”或“ID”标识符的特定部分
【发布时间】:2021-05-29 15:38:24
【问题描述】:

我在网页抓取时遇到问题

在 Python 中使用 BeautifulSuop4 时的

标签元素。通常,元素会被赋予一个类或 id 标识符,我可以在其中使用:

.find_all(<p>, class_ = 'class-name')

找到元素但是我试图隔离的元素在一个连续的列表中

标签,所有这些标签都没有其元素的标识符。

有没有办法选择每一个

标记在具有标识符的标记之后?或者也许是一种隔离特定的方法

我想要的标签没有任何共享类/ID?

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    您可以使用 find_next_sibling 来查找元素的无类下一个兄弟。

    考虑这个 HTML 示例。第一个 div 有“blah”类。第二个 div 没有类,但在第一个 div 旁边。

    html='<div><div class="blah">1</div><div>no class</div></div>'
    import bs4
    soup = bs4.BeautifulSoup(html,'html.parser')
    soup.find('div',{'class':"blah"}).find_next_sibling()
    
    #outputs second div without a class
    <div>no class</div>
    

    有关详细信息,请参阅 thisthis

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 2020-09-26
      相关资源
      最近更新 更多