代码之家  ›  专栏  ›  技术社区  ›  Alexander Engelhardt

如何提取特定标题后面的HTML表?

  •  3
  • Alexander Engelhardt  · 技术社区  · 6 年前

    我正在使用BeautifulSoup解析HTML文件。我有一个类似的HTML文件:

    <h3>Unimportant heading</h3>
    <table class="foo">
      <tr>
        <td>Key A</td>
      </tr>
      <tr>
        <td>A value I don't want</td>
      </tr>
    </table>
    
    
    <h3>Unimportant heading</h3>
    <table class="foo">
      <tr>
        <td>Key B</td>
      </tr>
      <tr>
        <td>A value I don't want</td>
      </tr>
    </table>
    
    
    <h3>THE GOOD STUFF</h3>
    <table class="foo">
      <tr>
        <td>Key C</td>
      </tr>
      <tr>
        <td>I WANT THIS STRING</td>
      </tr>
    </table>
    
    
    <h3>Unimportant heading</h3>
    <table class="foo">
      <tr>
        <td>Key A</td>
      </tr>
      <tr>
        <td>A value I don't want</td>
      </tr>
    </table>
    

    我要提取字符串“我要这个字符串”。最好的解决办法是 H3标题后面的第一张表叫做“好东西” . 我不知道如何用漂亮的汤来做这个-我只知道如何用一个特定的类或一个表来提取一个表。 嵌套内 一些特别的标签,但不是 下列的 一个特殊的标签。

    我认为回退解决方案可以使用字符串“key c”,假设它是唯一的(几乎可以肯定是唯一的),并且只出现在一个表中,但是我会觉得使用特定的h3标题更好。

    3 回复  |  直到 6 年前
        1
  •  1
  •   PythonSherpa    6 年前

    遵循@zroq的逻辑 answer 在另一个问题上,此代码将为您提供定义的头后面的表(“好东西”)。请注意,我只是把所有的HTML放在一个名为“HTML”的变量中。

    from bs4 import BeautifulSoup, NavigableString, Tag
    
    soup=BeautifulSoup(html, "lxml")
    
    for header in soup.find_all('h3', text=re.compile('THE GOOD STUFF')):
        nextNode = header
        while True:
            nextNode = nextNode.nextSibling
            if nextNode is None:
                break
            if isinstance(nextNode, Tag):
                if nextNode.name == "h3":
                    break
                print(nextNode)
    

    输出:

    <table class="foo">
    <tr>
    <td>Key C</td>
    </tr>
    <tr>
    <td>I WANT THIS STRING</td>
    </tr>
    </table>
    

    干杯!

        2
  •  0
  •   J_H    6 年前

    这个 docs 如果你不想使用 find_all ,您可以这样做:

    for sibling in soup.a.next_siblings:
        print(repr(sibling))
    
        3
  •  0
  •   Leo_28    6 年前

    我相信有很多方法可以更有效地做到这一点,但我现在可以考虑的是:

    from bs4 import BeautifulSoup
    import os
    os.chdir('/Users/Downloads/')
    html_data = open("/Users/Downloads/train.html",'r').read()
    soup = BeautifulSoup(html_data, 'html.parser')
    all_td = soup.find_all("td")
    flag = 'no_print'
    for td in all_td:
        if flag == 'print':
            print(td.text)
            break
        if td.text == 'Key C':
            flag = 'print'
    

    输出:

    I WANT THIS STRING