我试图从网页中提取一个表格。下面是使用beauthulsoup的HTML和Python代码。下面的代码一直对我有效,但在这种情况下,我得到了空白。提前谢谢。在
<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>
^{pr2}$
补充了@abarner指出的。我将得到所有以
$
开头的文本的td
元素:对于您提供的输入,它将打印:
^{pr2}$您可以将其“解包”为单独的变量:
请注意,我显式地传递
recursive=False
,以避免在树中更深入,只获得table
元素的直接tr
子元素。在您的第一个问题是^{} (或} argument 文档所说的那样,请通过
findAll
,这只是同一事物的一个不推荐使用的同义词)不只是查找表中的行,而是查找表中的行以及表中的每个子表中的行。几乎可以肯定的是,您不希望迭代两种类型的行并在每一行上运行相同的代码。如果您不想这样,就像the ^{recursive=False
。在所以,现在你只能回来一排了。如果你做了
row.find_all('td')
,那又会有同样的问题,你将找到这一行的所有列,以及其中一列中每个子表中每一行的所有列。同样,这不是您想要的,所以使用recursive=False
。在现在你只得到了5列。第一张是一张大桌子,里面有一堆空单元格;另一方面,另一方面,另一方面,里面有美元的价值,这似乎是你想要的。在
所以,只需将
recursive=False
添加到两个调用中,并将stock
设置为某个值(我不知道它在代码中应该来自何处,但是如果没有它,您显然只会得到一个NameError
):这将打印:
^{pr2}$我不知道为什么您两次使用
period3
而从未使用period2
,为什么完全跳过第1列,或者为什么要切片1元素列表而不是仅仅索引值,但是不管怎样,这似乎就是您要做的。在作为补充说明,如果您真的希望将列表分解为5个值,而不是分成4个单元素列表,跳过其中一个值,您可以写下:
相关问题 更多 >
编程相关推荐