python - 在 html 中搜索颜色标签(Python 3)

标签 python beautifulsoup

如果单元格具有某种颜色,我试图从表格中获取元素。唯一的问题是,对于颜色标签,抓取颜色似乎还不太可能。

jump = []

for tr in site.findAll('tr'):
  for td in site.findAll('td'):
    if td == 'td bgcolor':
      jump.append(td)

print(jump)

这将返回一个空列表

如何从下面的 html 中获取颜色?

我需要从 [td] 标签获取颜色(从 [tr] 标签获取颜色也很有用)

<tr bgcolor="#f4f4f4">
<td height="25" nowrap="NOWRAP">&nbsp;CME_ES&nbsp;</td>
<td height="25" nowrap="NOWRAP">&nbsp;07:58:46&nbsp;</td>
<td height="25" nowrap="NOWRAP">&nbsp;Connected&nbsp;</td>
<td height="25" nowrap="NOWRAP">&nbsp;0&nbsp;</td>
<td height="25" nowrap="NOWRAP">&nbsp;0&nbsp;</td>
<td height="25" nowrap="NOWRAP">&nbsp;0&nbsp;</td>
<td height="25" nowrap="NOWRAP">&nbsp;0&nbsp;</td>
<td height="25" nowrap="NOWRAP">&nbsp;07:58:00&nbsp;</td>
<td height="25" nowrap="NOWRAP" bgcolor="#55aa2a">&nbsp;--:--:--&nbsp;</td>
<td height="25" nowrap="NOWRAP">&nbsp;0&nbsp;</td>
<td height="25" nowrap="NOWRAP">&nbsp;0&nbsp;</td>
<td height="25" nowrap="NOWRAP">&nbsp;01:25:00 &nbsp;</td>
<td height="25" nowrap="NOWRAP">&nbsp; 22:00:00&nbsp;</td>
</tr>

最佳答案

这个怎么样:

jump = []

for tr in site.findAll('tr'):
  for td in site.findAll('td'):
    if 'bgcolor' in td.attrs:
      #jump.append(td)
      print(td.attrs['bgcolor'])

print(jump)

关于python - 在 html 中搜索颜色标签(Python 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53376712/

相关文章:

python - 使用 SageMaker 模型预测批量图像

python - South 不认识我的模型

python - 如何使用 BeautifulSoup 找到特定的视频 html 标签?

Python 'latin-1' 编解码器无法编码字符 - 如何忽略字符?

python - 无法在 virtualenv 中导入 beautifulsoup4

Python Data Scraping - 提取表中存在标签 '<td>' 的行

python - 在 Keras 中多次调用 "fit_generator()"

python - 按自定义键对字符串列表进行排序

python - 数据标准化

python - BeautifulSoup stripped_strings 中有太多换行符...如何使纯文本格式更接近原始 html?