运用 Beautiful Soup 对嵌套列表进行解析,并保持其结构

137 阅读3分钟

我们有一个包含 beautiful soup 对象的列表,需要进一步解析其内容。输出结果为一个列表的列表,每个子列表包含原始表格中的三列内容。

huake_00015_.jpg

file = <html><p><center><h1>  Interference Report  </h1></center><p>
<b>  Interference Report Project File:  </b>C:\Users\ksobon\Documents\test_project_03_ksobon.rvt  <br>  <b>  Created:  </b>  Monday, May 26, 2014 7:52:32 PM  <br>  <b>  Last Update:  </b>    <br>
 <p><table border=on>  <tr>  <td></td>  <td ALIGN="center">A</td>  <td  ALIGN="center">B</td>  </tr>
<tr>  <td>  1  </td>  <td>  Workset1 : Walls : Basic Wall : E103-CON 100mm : id 469021     </td>  <td>  Workset1 : Furniture : FUR_BoardroomTable10Chairs_gm : Board Room Layout : id   482259  </td>  </tr>
<tr>  <td>  2  </td>  <td>  Workset1 : Walls : Basic Wall : E103-CON 100mm : id 469021    </td>  <td>  Workset1 : Walls : Basic Wall : E103-CON 100mm : id 483442  </td>  </tr>
<tr>  <td>  3  </td>  <td>  Workset1 : Walls : Basic Wall : E103-CON 100mm : id 469060    </td>  <td>  Workset1 : Furniture : FUR_Sofa_gm : 2100mm : id 475041  </td>  </tr>
<tr>  <td>  4  </td>  <td>  Workset1 : Walls : Basic Wall : E103-CON 100mm : id 469109   </td>  <td>  Workset1 : Furniture : FUR_Sofa_gm : 2100mm : id 475273  </td>  </tr>
<tr>  <td>  5  </td>  <td>  Workset1 : Walls : Basic Wall : E103-CON 100mm : id 469178   </td>  <td>  Workset1 : Furniture : FUR_Sofa_gm : 2100mm : id 475510  </td>  </tr>
<tr>  <td>  6  </td>  <td>  Workset1 : Walls : Basic Wall : E103-CON 100mm : id 469178    </td>  <td>  Workset1 : Furniture : FUR_Sofa_gm : 2100mm : id 482306  </td>  </tr>
<tr>  <td>  7  </td>  <td>  whatever : Doors : DOR_Single_gm : 800w, 2100h (720Leaf) -  Mark 102B : id 472052  </td>  <td>  Workset1 : Windows : WIN-ConceptWindowFixed_gm : 1200 H   x 1200 W - Mark 102B : id 472822  </td>  </tr>
<tr>  <td>  8  </td>  <td>  whatever : Doors : DOR_Single_gm : 800w, 2100h (720Leaf) -  Mark 101A : id 472376  </td>  <td>  Workset1 : Windows : WIN-ConceptWindowFixed_gm : 1200 H   x 1200 W - Mark 101C : id 472720  </td>  </tr>
<tr>  <td>  9  </td>  <td>  Workset1 : Windows : WIN-ConceptWindowFixed_gm : 1800 H x  1200 W 2 - Mark 101B : id 472688  </td>  <td>  Workset1 : Furniture : FUR_Sofa_gm : 2100mm   : id 482306  </td>  </tr>
</table>
<p><b>  End of Interference Report  </b>
</html>

从 Beautiful Soup 对象中提取数据:

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(file)
tag = soup.findAll('tr')
txt=[(i.findAll('td')) for i in tag]

将每个子列表转换成文本数据:

txt1 = [[i.text for i in x] for x in txt]

问题:提取的数据变成了一个单一列表,而不是一个包含列表的列表。

2、解决方案

为了解决这个问题,我们需要对提取数据的列表进行结构化,使之保持原始表的格式:

final=[[] for x in range(len(txt))]
for j,k in enumerate(txt):
    for i in k:
        final[j].append(i.text)

现在,final 变量包含了一个列表的列表,每个子列表表示原始表中的一行。我们可以打印 final 来查看结果:

print (final)
# Output:
[[u'', u'A', u'B'], [u'1', u'Workset1 : Walls : Basic Wall : E103-CON 100mm : id 469021', u'Workset1 : Furniture : FUR_BoardroomTable10Chairs_gm : Board Room Layout......

该解决方案使我们能够根据需要对 BeautifulSoup 对象的数据进行进一步的处理和分析。