我想从网页中提取表格。下面是使用beautifulsoup的HTML和Python代码。下面的代码总是为我工作,但在这种情况下,我变得空白。提前致谢。使用Python提取HTML表格BeautifulSoup
<table>
<thead>
<tr>
<th>Period Ending:</th>
<th class="TalignL">Trend</th>
<th>9/27/2014</th>
<th>9/28/2013</th>
<th>9/29/2012</th>
<th>9/24/2011</th>
</tr>
</thead>
<tr>
<th bgcolor="#E6E6E6">Total Revenue</th>
<td class="td_genTable"><table border="0" align="center" width="*" cellspacing="0" cellpadding="0"><tr><td align="bottom"><table border="0" height="100%" cellspacing="0" cellpadding="0"><tr><td><table cellspacing="0" cellpadding="0" border="0"><tr><td height="15" bgcolor="#47C3D3" width="6"></td><td height="15" bgcolor="#FFFFFF" width="1px"></td></tr><tr><td height="1" colspan="2" bgcolor="#D1D1D1"></td></tr></table></td><td><table cellspacing="0" cellpadding="0" border="0"><tr><td height="1" bgcolor="#FFFFFF" width="6"></td><td height="1" bgcolor="#FFFFFF" width="1px"></td></tr><tr><td height="14" bgcolor="#47C3D3" width="6"></td><td height="14" bgcolor="#FFFFFF" width="1px"></td></tr><tr><td height="1" colspan="2" bgcolor="#D1D1D1"></td></tr></table></td><td><table cellspacing="0" cellpadding="0" border="0"><tr><td height="2" bgcolor="#FFFFFF" width="6"></td><td height="2" bgcolor="#FFFFFF" width="1px"></td></tr><tr><td height="13" bgcolor="#47C3D3" width="6"></td><td height="13" bgcolor="#FFFFFF" width="1px"></td></tr><tr><td height="1" colspan="2" bgcolor="#D1D1D1"></td></tr></table></td><td><table cellspacing="0" cellpadding="0" border="0"><tr><td height="7" bgcolor="#FFFFFF" width="6"></td><td height="7" bgcolor="#FFFFFF" width="1px"></td></tr><tr><td height="8" bgcolor="#47C3D3" width="6"></td><td height="8" bgcolor="#FFFFFF" width="1px"></td></tr><tr><td height="1" colspan="1" bgcolor="#D1D1D1"></td></tr></table></td></tr></table></td></tr></table></td>
<td>$182,795,000</td>
<td>$170,910,000</td>
<td>$156,508,000</td>
<td>$108,249,000</td>
rows = table.findAll('tr')
for row in rows:
cols = row.findAll('td')
col1 = [ele.text.strip().replace(',','') for ele in cols]
account = col1[0:1]
period1 = col1[2:3]
period2 = col1[3:4]
period3 = col1[4:5]
record = (stock, account,period1,period3,period3)
print record
你第一个非标题行的第一列包含一个充满空单元格的表格,其中没有文本。你的代码正确地发现没有文字。我不确定你想要它做什么。 – abarnert
同时,您为什么使用不推荐的名称'findAll'?您是否从针对BS3编写的示例代码学习,而不是从BS4的更新示例或文档中学习? – abarnert
最后,['find_all'](http://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-all)(或'findAll')搜索_all后代_,而不仅仅是顶级子代。因此,除非您想遍历外部表的行和嵌入在该表的列中的每个子表的行并将它们对待,否则您不应该在此处使用它。 – abarnert