如何使用 Beautifulsoup 干净地解析出 HTML 列表中的所有项目符号和子项目符号?

问题描述 投票:0回答:1

我有一个看起来像这样的文档

标题1

  • 该病毒已导致 56 人死亡
  • 全球媒体重点报道数百只狗在跳跃
    • 一位 Twitter 用户发布了猫的照片

标头2

  • 拜登总统周日发表讲话。

  • 你好世界

  • 我能为您提供什么帮助吗?

标题 3

  • 加沙战争仍在继续
  • 全球媒体重点报道最好的披萨
    • 一位 Twitter 用户发布了寿司
    • 一位 Twitter 用户发布了糖果

我将其转换为 HTML,它看起来像这样

<html>
 <body>
 <h1>HEADER1</h1>
  <ul>
   <li>
    the virus killed 56
   </li>
   <li>
    Global press
    <a href="https://www.example.com">
     highlight
    </a>
    hundreds of dogs jumping
    <ul>
     <li>
      A Twitter user
      <a href="http://example.com/xad/status/sda">
       posts
      </a>
      photos of cats
     </li>
    </ul>
   </li>
  </ul>
 <h1>HEADER2</h1>
  <ul>
   <li>
    President Biden talks on Sunday.
   </li>
   Hello World
   <li>
    How can I help you?
   </li>
  </ul>
 <h1>HEADER3</h1>
 <ul>
   <li>
    The war in Gaza continues
   </li>
   <li>
    Global press highlights best pizza
    <ul>
     <li>
      A Twitter user posts sushi
     </li>
     <li>
      A Twitter user posts candy
     </li>
    </ul>
   </li>
  </ul>
 </body>
</html>

由于项目符号嵌套在列表中的方式以及列表本身就是列表对象,我一生都无法弄清楚如何解析出外部 ul 标签中的每个单独的

  • ...
  • 标签或内部 ul 标签。

    我想要的最终结果看起来像这样

    header1_posts = [

  • 病毒杀死了56人
  • 全球新闻 s亮点数百只狗在跳跃
  • 推特用户帖子猫的照片
  • ]

    header2_posts = [...]

    header3_posts = [...]

    我尝试了 find、find_all、find_all_next 的所有组合并迭代不同的元素,但最终没有成功。

    谢谢

    html python-3.x parsing beautifulsoup
    1个回答
    0
    投票

    你可以尝试:

    from bs4 import BeautifulSoup
    
    html_text = """\
    <html>
     <body>
     <h1>HEADER1</h1>
      <ul>
       <li>
        the virus killed 56
       </li>
       <li>
        Global press
        <a href="https://www.example.com">
         highlight
        </a>
        hundreds of dogs jumping
        <ul>
         <li>
          A Twitter user
          <a href="http://example.com/xad/status/sda">
           posts
          </a>
          photos of cats
         </li>
        </ul>
       </li>
      </ul>
     <h1>HEADER2</h1>
      <ul>
       <li>
        President Biden talks on Sunday.
       </li>
       Hello World
       <li>
        How can I help you?
       </li>
      </ul>
     <h1>HEADER3</h1>
     <ul>
       <li>
        The war in Gaza continues
       </li>
       <li>
        Global press highlights best pizza
        <ul>
         <li>
          A Twitter user posts sushi
         </li>
         <li>
          A Twitter user posts candy
         </li>
        </ul>
       </li>
      </ul>
     </body>
    </html>"""
    
    soup = BeautifulSoup(html_text, "html.parser")
    
    
    def get_li_without_ul(li):
        soup = BeautifulSoup(str(li), "html.parser")
        for ul in soup.find_all("ul"):
            ul.extract()
        return soup
    
    
    out = {}
    for li in soup.find_all("li"):
        header = li.find_previous("h1")
        out.setdefault(header.text.strip(), []).append(get_li_without_ul(li))
    
    print(out)
    

    打印:

    {'HEADER1': [<li>
        the virus killed 56
       </li>, <li>
        Global press
        <a href="https://www.example.com">
         highlight
        </a>
        hundreds of dogs jumping
        
    </li>, <li>
          A Twitter user
          <a href="http://example.com/xad/status/sda">
           posts
          </a>
          photos of cats
         </li>], 'HEADER2': [<li>
        President Biden talks on Sunday.
       </li>, <li>
        How can I help you?
       </li>], 'HEADER3': [<li>
        The war in Gaza continues
       </li>, <li>
        Global press highlights best pizza
        
    </li>, <li>
          A Twitter user posts sushi
         </li>, <li>
          A Twitter user posts candy
         </li>]}
    
    © www.soinside.com 2019 - 2024. All rights reserved.