【问题标题】:How to get all elements of nested tags in BeautifulSoup?如何在 BeautifulSoup 中获取嵌套标签的所有元素?
【发布时间】:2021-09-29 22:14:15
【问题描述】:

我对 HTML 和解析非常陌生,因此如果我为此使用了错误的术语,我深表歉意。我之前问了一个类似的问题,并找到了一些有用的答案。我有以下 HTML sn-p 包含两个表和两个表头(以及更多行但与这篇文章无关)

<body>
    <table>
        <tr class="header">
            <th><strong>Heading 1</strong></th>
            <th><strong>Heading 2</strong></th>
            <th><strong>Heading 3</strong></th>
            <th><p><strong>Heading 4, line 1</strong></p>
            <p><strong>Heading 4, line 2</strong></p></th>
        </tr>

        <tr>
            <!--Many more rows-->>
        </tr>
    </table>

    <table>
        <tr class="header">
            <th><strong>Diff Header 1</strong></th>
            <th><strong>Diff Header 2</strong></th>
            <th><strong>Diff Header 3</strong></th>
            <th><p><strong>Diff Header 4, line 1</strong></p>
            <p><strong>Diff Header 4, line 2</strong></p></th>
        </tr>
    
        <tr>
            <!--Many more rows-->>
        </tr>
    </table>
</body>

我正在尝试使用 python3.6 和 BeautifulSoup4 来解析它并将文本提取到一个列表中。我的问题是,我希望每个块都有单独的列表。我当前的代码似乎搜索并找到了所有 &lt;th&gt; 标签,而不是第一个表中的标签。

这是我所拥有的:

def parse_html(self):
    """ Parse the html file """
    with open(self.html_path) as f:
        soup = BeautifulSoup(f, 'html.parser')

    tables = soup.find_all('table')
    
    for table in tables:
        # Find each row in the table
        rows = table.find_all_next('tr')
        for row in rows:
            # Find each column in the row
            cols = row.find_all_next('th')
            for col in cols:
                # Print each cell
                print(col) # This is where it seems to be finding every <th>

            break          # Break just to do the first row (seems not to work?)

问题:如何修改此代码,使其仅在当前行而不是每一行中找到 &lt;th&gt; 标签?

感谢您的帮助!

【问题讨论】:

    标签: python html parsing beautifulsoup


    【解决方案1】:

    使用.find_all 而不是.find_all_next

    如果html_doc 是问题中的 HTML sn-p:

    soup = BeautifulSoup(html_doc, "html.parser")
    
    tables = soup.find_all("table")
    
    for table in tables:
        # Find each row in the table
        rows = table.find_all("tr")
        for row in rows:
            cols = row.find_all("th")
            for col in cols:
                print(col)
    
        print("-" * 80)
    

    打印:

    <th><strong>Heading 1</strong></th>
    <th><strong>Heading 2</strong></th>
    <th><strong>Heading 3</strong></th>
    <th><p><strong>Heading 4, line 1</strong></p>
    <p><strong>Heading 4, line 2</strong></p></th>
    --------------------------------------------------------------------------------
    <th><strong>Diff Header 1</strong></th>
    <th><strong>Diff Header 2</strong></th>
    <th><strong>Diff Header 3</strong></th>
    <th><p><strong>Diff Header 4, line 1</strong></p>
    <p><strong>Diff Header 4, line 2</strong></p></th>
    --------------------------------------------------------------------------------
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      • 2013-02-02
      • 2011-06-03
      • 1970-01-01
      相关资源
      最近更新 更多