首页 文章

使用BeautifulSoup在python中解析带有img标记的表

提问于
浏览
0

我正在使用BeautifulSoup来解析一个html页面 . 我需要处理页面中的第一个表 . 该表包含几行 . 然后每行包含一些'td'标记,其中一个'td'标记具有'img'标记 . 我想获得该表中的所有信息 . 但如果我打印那张 table ,我就不会't get any data related to the ' img'标签 .

我使用soap.findAll(“table”)来获取所有表,然后选择第一个表进行处理 . html看起来像这样:

<table id="abc"
  <tr class="listitem-even">
    <td class="listitem-even">
      <table border = "0"> <tr> <td class="gridcell">
               <img id="img_id" title="img_title" src="img_src" alt="img_alt" /> </td> </tr>
      </table>
    </td>
    <td class="listitem-even"
      <span>some_other_information</span>
    </td>
  </tr>
</table>

如何获取表格中的所有数据,包括'img'标签?谢谢,

1 回答

  • 1

    您有一个嵌套表,因此在解析tr / td / img标记之前,您需要检查树中的位置 .

    from bs4 import BeautifulSoup
    f = open('test.html', 'rb')
    html = f.read()
    f.close()
    soup = BeautifulSoup(html)
    
    tables = soup.find_all('table')
    
    for table in tables:
         if table.find_parent("table") is not None:
             for tr in table.find_all('tr'):
                     for td in table.find_all('td'):
                             for img in td.find_all('img'):
                                     print img['id']
                                     print img['src']
                                     print img['title']
                                     print img['alt']
    

    它根据您的示例返回以下内容:

    img_id
    img_src
    img_title
    img_alt
    

相关问题