【问题标题】:Use beautifulSoup to find a table after a header?使用beautifulSoup 查找表头后的表格?
【发布时间】:2021-06-11 13:41:23
【问题描述】:

我正在尝试从网站上抓取一些数据。我想要的数据列在一个表中,但是有多个表并且没有 ID。然后我想到我会在我正在搜索的表格上方找到标题,然后将其用作指标。

这真的很困扰我,所以作为最后的手段,我想问是否有人知道如何用 BeautifulSoup 来找到桌子。 下面提供了 HTML 代码的片段,在此先感谢 :)

我感兴趣的表格是<h2>Mine neaste vagter</h2>正下方的表格

        <h2>Min aktuelle vagt</h2>
        
        
            <div>
                <a href='/shifts/detail/595212/'>Flere detaljer</a>
            <p>Vagt starter: <b>11/06 2021 - 07:00</b></p>
            <p>Vagt slutter: <b>11/06 2021 - 11:00</b></p>

            

            

            
                <h2>Masker</h2>
                <table class='list'>
                    <tr><th>Type</th><th>Fra</th><th>&nbsp;</th><th>Til</th></tr>
                    
                    <tr>
                        <td>Fri egen regningD</td>
                        <td>07:00</td>
                        <td>&nbsp;-&nbsp;</td>
                        <td>11:00</td>
                    </tr>
                    
                </table>
            
            </div>
        
    <hr>
    
    
    
    
    
    


    
    
    
    
    
    




    
        <h2>Mine neaste vagter</h2>
        <table class='list'>
            <tr>
                <th class="alignleft">Dato</th>
                <th class="alignleft">Rolle</th>
                <th class="alignleft">Tidsrum</th>
                <th></th>
                <th class="alignleft">Bytte</th>
                <th class="alignleft" colspan='2'></th>
            </tr>
            
                <tr class="rowA separator">
                    
                        <td>
                            <h3>12/6</h3>
                        </td>
                    
                    <td>Kundeservice</td>
                    <td>18:00 &rarr; 21:30 (3.5 t)</td>
                    <td style="max-width: 20em;"></td>

                    <td>
                      
                        <a href="/shifts/ajax/popup/595390/" class="swap shiftpop">
                          Byt denne vagt
                        </a>
                      
                    </td>
                    
                    <td><a href="/shifts/detail/595390/">Detaljer</td>
                      
                      <td>
                        
                          &nbsp;
                        
                    </td>
                </tr>

【问题讨论】:

  • 如果可能的话,您能否分享 URL,或者您可以 find_all h2 标签并从最后一个 h2 标签使用 find_next 提取表数据

标签: python python-3.x web-scraping beautifulsoup


【解决方案1】:

这里有两种方法可以找到正确的&lt;table&gt;

  1. 由于您想要的表格是 HTML 中的最后一个表格,因此您可以使用 find_all() 并使用索引切片 [-1] 找到最后一个表格:

    print(soup.find_all("table", class_="list")[-1])

  2. 通过文本查找h2元素,并使用find_next()方法查找表:

    print(soup.find(lambda tag: tag.name == "h2" and "Mine neaste vagter" in tag.text).find_next("table"))

【讨论】:

    【解决方案2】:

    您可以使用:-soup-contains(或只是:contains)通过其文本定位&lt;h2&gt;,然后使用find_next 移动到表格:

    from bs4 import BeautifulSoup as bs
    
    html = '''your html'''
    soup = bs(html,  'lxml')
    soup.select_one('h2:-soup-contains("Mine neaste vagter")').find_next('table')
    

    这是假设 HTML(如图所示)由您使用的任何访问方法返回。

    【讨论】:

      猜你喜欢
      • 2014-06-28
      • 2022-07-18
      • 1970-01-01
      • 1970-01-01
      • 2012-04-04
      • 2017-07-07
      • 1970-01-01
      • 2011-03-11
      • 2017-02-15
      相关资源
      最近更新 更多