【问题标题】:perform BeautifulSoup operation on list of lists while maintaining structure in python对列表列表执行 BeautifulSoup 操作,同时在 python 中保持结构
【发布时间】:2014-07-15 18:35:27
【问题描述】:

我有一个漂亮的汤对象列表,我正在尝试进一步解析单元格的内容。我的输出变成了一个列表,每个列表有 3 个项目,因为该表有 3 列。

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>

从 BeautifulSoup 导入 BeautifulSoup 汤 = BeautifulSoup(文件) tag = soup.findAll('tr')

for i in tag:
    txt.append(i.findAll('td'))

现在我想将每个子列表元素转换为文本,所以我尝试了: txt1 = [i.text for x in txt for i in x] 然而,我的 txt1 输出以平面列表而不是列表的形式出现。我究竟做错了什么?

【问题讨论】:

    标签: python list beautifulsoup


    【解决方案1】:

    i.text 放入列表中:

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

    您正在使用列表理解将所有元素提取到一个列表中来展平列表。

    l = [[1,2],[2,3],[5,6]]
    
    flatten_l = [x for y in l for x in y]
    print (flatten_l)
    [1, 2, 2, 3, 5, 6]
    

    也许你需要地图:

    l=[[1,2,4],[2,3,5],[5,6,7]]
    
    print [map(str, s) for s in l]
    
    [['1', '2', '4'], ['2', '3', '5'], ['5', '6', '7']]
    

    使用您的代码在维护结构的每个元素上调用 i.text。

    from BeautifulSoup import BeautifulSoup
    
    soup = BeautifulSoup(file)
    
    tag = soup.findAll('tr')
    txt=[(i.findAll('td')) for i in tag]
    final=[[] for x in range(len(txt))]
    for j,k in enumerate(txt):
        for i in k:
            final[j].append(i.text)  
    
     print final
     [[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......
    

    【讨论】:

    • 好吧,txt 的结果是一个列表列表,每个列表包含 3 个项目,因为每个表中有 3 列。我想保持该结构,但将每个对象转换为字符串。您将 i.text 放入括号的建议为我提供了每个项目的单独列表。所以我想我需要问我如何在不改变其原始结构的情况下迭代列表列表。
    • 地图功能。我需要记住它是这样做的。谢谢!
    • 还有一件事。我真的很想使用来自 beautifulsoup 模块的函数文本,而你建议使用 str。当我 sub str for text 时,它会引发一个错误,即未定义文本。想法?
    • 尝试使用循环而不是 list comp 并调用每个元素的方法来更新它们,您可以使用 map(i.text,j) 之类的东西
    • for i in txt: txt1.append(map(i.text, txt)) 这仍然不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 2021-05-24
    • 2023-03-17
    相关资源
    最近更新 更多