【问题标题】:Python BeautifulSoup: Getting table elements based on color attributePython BeautifulSoup:根据颜色属性获取表格元素
【发布时间】:2018-11-28 17:57:13
【问题描述】:

我有一些看起来像这样的 html(这表示表中的数据行)

<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>
.
.
.
<tr bgcolor="#ffffff">
<td height="25" nowrap="NOWRAP">&nbsp;CME_NQ&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;191&nbsp;</td>
<td height="25" nowrap="NOWRAP">&nbsp;0&nbsp;</td>
<td height="25" nowrap="NOWRAP">&nbsp;07:58:01&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>

我有从每个数据集中获取颜色的代码:

mrkt_stat = []
for td in site.findAll('td'):
 if 'bgcolor' in td.attrs:
  mrkt_stat.append(td.attrs['bgcolor'])

打印此列表将为我提供每行数据中所有颜色的列表,这很好。

我还有从每个数据集中抓取行数据的代码:

data = []
for row in site.findAll('tr'):
 cols = row.find_all('td')
 cols = [ele.text.strip() for ele in cols]
 data.append([ele for ele in cols if ele])

这也很好,它返回一个列表列表,每个子列表是我正在抓取的表中的一行数据。

但是,我想附加/组合此代码,以便仅在行集中的十六进制颜色代码为“#55aa2a”时将信息添加到 data 列表中

编辑:当我将返回的列表输入到 pandas DataFrame 时,需要剥离返回的数据,而这个 DataFrame 就是将要呈现的内容

【问题讨论】:

    标签: python-3.x beautifulsoup


    【解决方案1】:

    使用attrs={'tag': 'attr'} 参数和site.find_all() 过滤行中的td

    for row in site.find_all('tr'):
    if row.find_all('td', attrs={'bgcolor': '#55aa2a'}):
        data.append([str(td.text.strip()) for td in row.find_all('td')])
    

    data 列表应仅包含具有选定bgcolor 的元素:

    >>> print(data)
    [['CME_ES', '07:58:46', 'Connected', '0', '0', '0', '0', '07:58:00', '--:--:--', '0', '0', '01:25:00', '22:00:00'], 
     ['CME_NQ', '07:58:46', 'Connected', '0', '0', '191', '0', '07:58:01', '--:--:--', '0', '0', '01:25:00', '22:00:00']]
    

    【讨论】:

    • 是否可以剥离所有数据,只显示有用的信息? [ CME_NQ ,  07:58:46 , . ..] 这是返回的内容,但只需要 [CME_NQ, 07:58:46, ...]
    • 是的,我明白了,很好,感谢您的反馈。检查更新。
    • 是的,每一行的子列表就是我要找的
    • 好的,它现在可以工作了。请再次检查更新。谢谢。
    • 如果我想在其中一行没有 bgcolor 属性的情况下从这里刮取相同的信息,那将如何工作?因此,如果行集中有一行看起来像  --:--:--  我将如何获取这个 NULL bgcolor 值?
    【解决方案2】:

    试试下面的

    for row in site.findAll('tr'):
      if row.attrs['bgcolor'] != '#55aa2a': continue
      cols = row.find_all('td')
      data.append([ele.text.strip() for ele in cols if ele.text.strip()])
    

    【讨论】:

    • 这个问题是编译器没有将'bgcolor'识别为有效切片,所以if语句的行失败
    【解决方案3】:

    您可以在单个嵌套列表推导中执行此操作:

    data = [i.text for row in soup.find_all('tr') for i in row.find_all('td', {'bgcolor': '#55aa2a'})]
    

    返回:

    ['\xa0--:--:--\xa0', '\xa0--:--:--\xa0']
    

    【讨论】:

    • 我喜欢这个,但我需要返回整行数据,即需要显示 [CME_NQ, 07:58:46, Connected, ...]。
    猜你喜欢
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 2012-09-30
    相关资源
    最近更新 更多