【问题标题】:How to extract deeply nested <p> tags using Beautiful Soup如何使用 Beautiful Soup 提取深度嵌套的 <p> 标签
【发布时间】:2021-12-28 20:59:14
【问题描述】:

我有以下内容,我正在尝试了解如何使用 Beautiful Soup 提取 &lt;p&gt; 标签副本(我对其他方法持开放态度)。如您所见,&lt;p&gt; 标签并非都嵌套在同一个&lt;div&gt; 中。我用下面的method 试了一下,但这似乎只有在两个&lt;p&gt; 标签都在同一个容器中时才有效。

<div class="top-panel">
  <div class="inside-panel-0">
    <h1 class="h1-title">Some Title</h1>
  </div>
  <div class="inside-panel-0">
    <div class="inside-panel-1">
      <p> I want to extract this copy</p>
    </div>
    <div class="inside-panel-1">
      <p>I want to extract this copy</p>
    </div>
  </div>
</div>

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    IIUC 尝试

    from bs4 import BeautifulSoup
    
    html = """<div class="top-panel">
      <div class="inside-panel-0">
        <h1 class="h1-title">Some Title</h1>
      </div>
      <div class="inside-panel-0">
        <div class="inside-panel-1">
          <p> I want to extract this copy</p>
        </div>
        <div class="inside-panel-1">
          <p>I want to extract this copy</p>
        </div>
      </div>
    </div>"""
    
    soup = BeautifulSoup(html, 'lxml')
    # find all the p tags that have a parent class of inside-panel-1
    soup.findAll({'p': {'class': 'inside-panel-1'}})
    
    [<p> I want to extract this copy</p>, <p>I want to extract this copy</p>]
    

    如果你只想要文字,那么试试

    p_tags = soup.findAll({'p': {'class': 'inside-panel-1'}})
    [elm.text for elm in p_tags]  
    # -> [' I want to extract this copy', 'I want to extract this copy']
    

    【讨论】:

    • 在检查了提供的解决方案并进一步思考了我的困境之后,该页面将包含许多类 inside-panel-1 在这种情况下,有没有办法从类 top-panel 中获取 &lt;p&gt; 标签?
    【解决方案2】:

    由于p标签在div class="inside-panel-1内部,所以我们可以通过调用find_all方法轻松抓取它们,如下所示:

    from bs4 import BeautifulSoup
    
    html = """
    <div class="top-panel">        
     <div class="inside-panel-0">  
      <h1 class="h1-title">        
       Some Title
      </h1>
     </div>
     <div class="inside-panel-0">  
      <div class="inside-panel-1"> 
       <p>
        I want to extract this copy
       </p>
      </div>
      <div class="inside-panel-1"> 
       <p>
        I want to extract this copy
       </p>
      </div>
     </div>
    </div>
    
    """
    
    soup = BeautifulSoup(html, 'html.parser')
    # print(soup.prettify())
    
    p_tags = soup.select('div.top-panel div[class="inside-panel-1"]')
    for p_tag in p_tags:
        print(p_tag.get_text(strip=True))
    

    输出:

    I want to extract this copy
    I want to extract this copy
    

    【讨论】:

    • 有没有办法从top-panel 类中获取&lt;p&gt; 标签?该页面将包含inside-panel-1 的各种实例,我不需要所有实例,只需要来自top-panel 的实例。
    • @Freddy,我已经使用 css 选择器进行了更新。你可以试试这个。谢谢
    • 它也有效。 p_tags = soup.select('div.top-panel div[class="inside-panel-1"]>p')
    • Fazlul,我能够按照您的示例并通过使用以下 soup.select("div.top-panel p") - (CSS Selectors) 进行更多简化。谢谢你。
    猜你喜欢
    • 2020-08-24
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2021-12-04
    • 1970-01-01
    相关资源
    最近更新 更多