I have some html that looks like this (this represents rows of data in a table, i.e the data between tr and /tr is one row in a table)
<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>
I have code that grabs the color from each row set:
mrkt_stat = []
for td in site.findAll('td'):
if 'bgcolor' in td.attrs:
mrkt_stat.append(td.attrs['bgcolor'])
Issue is that when the row set has no bgcolor attribute, no data is added to mrkt_stat list.
How do I scrape this so that even if a row has no bgcolor attr, it will still be added to the list as NULL or N/A?
It is useful to know that the bgcolor attr (that may or may not be present) will always appear in the 9th line of a row set whether that row has the attr or not (look at the html lines enclosed with **)
EDIT: Output should look like the following (a list of all color attrs from row 9 of each row set and display 'N/A' if there is no color attr present):
['#55aa2a',...,'N/A']
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…