从html文件中删除空表

从html文件中删除空表,html,python-2.7,Html,Python 2.7,我有一个HTML文件,其中包含包含SQL O/p的表。但在每一张想要的表格之后,我也会得到一张空白表格,我打算把它删除 空白表编码为- <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> </tr> </thead> <tbod

我有一个HTML文件,其中包含包含SQL O/p的表。但在每一张想要的表格之后,我也会得到一张空白表格,我打算把它删除

空白表编码为-

<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
HTML文件类似-(仅显示部分文件)

2个缺少数据的数据类(*仅检查最近5天)
日期
数据类
失踪人数
预期的\u设备\u计数
预期的\u设备\u联机
缺少联机设备的\u计数\u
反对阿胡小组
日期
数据类
失踪人数
预期的\u设备\u计数
预期的\u设备\u联机
缺少联机设备的\u计数\u
3 tm_devicestorage中有多少设备的storageid不正确
日期
错误的\u存储\u id\u数据\u计数
上周错误的存储\u id\u数据\u计数\u
4.
(续)

我需要清除html文件中的空表。

这是假定的BeautifulSoup 4.7+

您应该能够使用选择器来识别非空表。在本例中,我们将使用一些特殊的伪类:
:is()
,它只允许我们对多个选择器进行分组
:has()
可以让我们提前查看特定元素的后代,
:empty
(在CSS级别4中指定)可以返回没有子元素的元素和没有文本的元素(空白除外),最后是
:not()
,可以查找不匹配的元素

我们将使用此选择器查找空表:
table:not(:has(:is(th,td):not(:empty))

它查找一个
,该表没有任何子体是
th
td
且不为空

我们以相反的顺序删除子项,以确保不会意外删除包含稍后要删除的子项的内容。这里不一定需要,但我这样做是出于习惯

from bs4 import BeautifulSoup

html = '''
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>date</th>
      <th>data_class</th>
      <th>missing_count</th>
      <th>expected_device_count</th>
      <th>expected_device_online</th>
      <th>missing_count_for_device_online</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table> Against ah_unit<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>date</th>
      <th>data_class</th>
      <th>missing_count</th>
      <th>expected_device_count</th>
      <th>expected_device_online</th>
      <th>missing_count_for_device_online</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>3 How many devices have bad storageid's in tm_devicestorage<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>date</th>
      <th>bad_storage_id_data_count</th>
      <th>bad_storage_id_data_count_last_week</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>
'''

soup = BeautifulSoup(html, 'html5lib')
for el in reversed(soup.select('table:not(:has(:is(th,td):not(:empty)))')):
    el.extract()

print(str(soup))

从bs4导入美化组
html=“”
日期
数据类
失踪人数
预期的\u设备\u计数
预期的\u设备\u联机
缺少联机设备的\u计数\u
反对阿胡小组
日期
数据类
失踪人数
预期的\u设备\u计数
预期的\u设备\u联机
缺少联机设备的\u计数\u
3 tm_devicestorage中有多少设备的storageid不正确
日期
错误的\u存储\u id\u数据\u计数
上周错误的存储\u id\u数据\u计数\u
'''
soup=BeautifulSoup(html,“html5lib”)
对于反向中的el(soup.select('table:not(:has(:is(th,td):not(:empty)))):
el.摘录()
打印(str(汤))
这就只剩下包含以下内容的表:

<html><head></head><body>                                                                                                                                                             

<table border="1" class="dataframe">                                                                                                                                                  
  <thead>                                                                                                                                                                             
    <tr style="text-align: right;">                                                                                                                                                   
      <th></th>                                                                                                                                                                       
      <th>date</th>                                                                                                                                                                   
      <th>data_class</th>                                                                                                                                                             
      <th>missing_count</th>                                                                                                                                                          
      <th>expected_device_count</th>                                                                                                                                                  
      <th>expected_device_online</th>                                                                                                                                                 
      <th>missing_count_for_device_online</th>                                                                                                                                        
    </tr>                                                                                                                                                                             
  </thead>                                                                                                                                                                            
  <tbody>                                                                                                                                                                             
  </tbody>                                                                                                                                                                            
</table> Against ah_unit                                                                                                                                                              

<table border="1" class="dataframe">                                                                                                                                                  
  <thead>                                                                                                                                                                             
    <tr style="text-align: right;">                                                                                                                                                   
      <th></th>                                                                                                                                                                       
      <th>date</th>                                                                                                                                                                   
      <th>data_class</th>                                                                                                                                                             
      <th>missing_count</th>                                                                                                                                                          
      <th>expected_device_count</th>                                                                                                                                                  
      <th>expected_device_online</th>                                                                                                                                                 
      <th>missing_count_for_device_online</th>                                                                                                                                        
    </tr>                                                                                                                                                                             
  </thead>                                                                                                                                                                            
  <tbody>                                                                                                                                                                             
  </tbody>                                                                                                                                                                            
</table>3 How many devices have bad storageid's in tm_devicestorage                                                                                                                   

<table border="1" class="dataframe">                                                                                                                                                  
  <thead>                                                                                                                                                                             
    <tr style="text-align: right;">                                                                                                                                                   
      <th></th>                                                                                                                                                                       
      <th>date</th>                                                                                                                                                                   
      <th>bad_storage_id_data_count</th>                                                                                                                                              
      <th>bad_storage_id_data_count_last_week</th>                                                                                                                                    
    </tr>                                                                                                                                                                             
  </thead>                                                                                                                                                                            
  <tbody>                                                                                                                                                                             
  </tbody>                                                                                                                                                                            
</table>                                                                                                                                                                              
</body></html>                                                                                                                                                                        


日期
数据类
失踪人数
预期的\u设备\u计数
预期的\u设备\u联机
缺少联机设备的\u计数\u
反对阿胡小组
from bs4 import BeautifulSoup

html = '''
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>date</th>
      <th>data_class</th>
      <th>missing_count</th>
      <th>expected_device_count</th>
      <th>expected_device_online</th>
      <th>missing_count_for_device_online</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table> Against ah_unit<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>date</th>
      <th>data_class</th>
      <th>missing_count</th>
      <th>expected_device_count</th>
      <th>expected_device_online</th>
      <th>missing_count_for_device_online</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>3 How many devices have bad storageid's in tm_devicestorage<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>date</th>
      <th>bad_storage_id_data_count</th>
      <th>bad_storage_id_data_count_last_week</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>
'''

soup = BeautifulSoup(html, 'html5lib')
for el in reversed(soup.select('table:not(:has(:is(th,td):not(:empty)))')):
    el.extract()

print(str(soup))

<html><head></head><body>                                                                                                                                                             

<table border="1" class="dataframe">                                                                                                                                                  
  <thead>                                                                                                                                                                             
    <tr style="text-align: right;">                                                                                                                                                   
      <th></th>                                                                                                                                                                       
      <th>date</th>                                                                                                                                                                   
      <th>data_class</th>                                                                                                                                                             
      <th>missing_count</th>                                                                                                                                                          
      <th>expected_device_count</th>                                                                                                                                                  
      <th>expected_device_online</th>                                                                                                                                                 
      <th>missing_count_for_device_online</th>                                                                                                                                        
    </tr>                                                                                                                                                                             
  </thead>                                                                                                                                                                            
  <tbody>                                                                                                                                                                             
  </tbody>                                                                                                                                                                            
</table> Against ah_unit                                                                                                                                                              

<table border="1" class="dataframe">                                                                                                                                                  
  <thead>                                                                                                                                                                             
    <tr style="text-align: right;">                                                                                                                                                   
      <th></th>                                                                                                                                                                       
      <th>date</th>                                                                                                                                                                   
      <th>data_class</th>                                                                                                                                                             
      <th>missing_count</th>                                                                                                                                                          
      <th>expected_device_count</th>                                                                                                                                                  
      <th>expected_device_online</th>                                                                                                                                                 
      <th>missing_count_for_device_online</th>                                                                                                                                        
    </tr>                                                                                                                                                                             
  </thead>                                                                                                                                                                            
  <tbody>                                                                                                                                                                             
  </tbody>                                                                                                                                                                            
</table>3 How many devices have bad storageid's in tm_devicestorage                                                                                                                   

<table border="1" class="dataframe">                                                                                                                                                  
  <thead>                                                                                                                                                                             
    <tr style="text-align: right;">                                                                                                                                                   
      <th></th>                                                                                                                                                                       
      <th>date</th>                                                                                                                                                                   
      <th>bad_storage_id_data_count</th>                                                                                                                                              
      <th>bad_storage_id_data_count_last_week</th>                                                                                                                                    
    </tr>                                                                                                                                                                             
  </thead>                                                                                                                                                                            
  <tbody>                                                                                                                                                                             
  </tbody>                                                                                                                                                                            
</table>                                                                                                                                                                              
</body></html>