我有一些类似这样的html(它表示表中的数据行,即tr和/tr之间的数据是表中的一行)
<tr bgcolor="#f4f4f4">
<td height="25" nowrap="NOWRAP"> CME_ES </td>
<td height="25" nowrap="NOWRAP"> 07:58:46 </td>
<td height="25" nowrap="NOWRAP"> Connected </td>
<td height="25" nowrap="NOWRAP"> 0 </td>
<td height="25" nowrap="NOWRAP"> 0 </td>
<td height="25" nowrap="NOWRAP"> 0 </td>
<td height="25" nowrap="NOWRAP"> 0 </td>
<td height="25" nowrap="NOWRAP"> 07:58:00 </td>
**<td height="25" nowrap="NOWRAP" bgcolor="#55aa2a"> --:--:-- </td>**
<td height="25" nowrap="NOWRAP"> 0 </td>
<td height="25" nowrap="NOWRAP"> 0 </td>
<td height="25" nowrap="NOWRAP"> 01:25:00 </td>
<td height="25" nowrap="NOWRAP"> 22:00:00 </td>
</tr>
.
.
.
<tr bgcolor="#ffffff">
<td height="25" nowrap="NOWRAP"> CME_NQ </td>
<td height="25" nowrap="NOWRAP"> 07:58:46 </td>
<td height="25" nowrap="NOWRAP"> Connected </td>
<td height="25" nowrap="NOWRAP"> 0 </td>
<td height="25" nowrap="NOWRAP"> 0 </td>
<td height="25" nowrap="NOWRAP"> 191 </td>
<td height="25" nowrap="NOWRAP"> 0 </td>
<td height="25" nowrap="NOWRAP"> 07:58:01 </td>
**<td height="25" nowrap="NOWRAP"> --:--:-- </td>**
<td height="25" nowrap="NOWRAP"> 0 </td>
<td height="25" nowrap="NOWRAP"> 0 </td>
<td height="25" nowrap="NOWRAP"> 01:25:00 </td>
<td height="25" nowrap="NOWRAP"> 22:00:00 </td>
</tr>
我有从每一行集合中获取颜色的代码:
mrkt_stat = []
for td in site.findAll('td'):
if 'bgcolor' in td.attrs:
mrkt_stat.append(td.attrs['bgcolor'])
问题是,当行集合没有bgcolor属性时,没有数据添加到mrkt_stat列表中。你知道吗
我该如何删除它,以便即使一行没有bgcolor attr,它仍然会作为NULL或N/a添加到列表中?你知道吗
知道bgcolor attr(可能存在也可能不存在)总是出现在行集合的第9行中是很有用的,不管该行是否有attr(请看用**括起来的html行)
编辑:输出应如下所示(每行集合第9行的所有颜色属性列表,如果没有颜色属性,则显示“N/a”):
['#55aa2a',...,'N/A']
我想出了解决这个问题的办法,虽然这是一个相当长的方法,但解决了这个问题
总结到第
h = color[0]
行,我将行集合的第9行存储到变量h
中,然后检查bgcolor是否在这个标记的属性中。如果是,则将其添加到check
列表中如果不是,则改为添加'N/A'
如果您能想出缩短此方法,我们将不胜感激:)!你知道吗
您可以将
else
语句添加到if
语句中:相关问题 更多 >
编程相关推荐