为了账号安全,请及时绑定邮箱和手机立即绑定

从 edgar 中抓取特定数据

从 edgar 中抓取特定数据

红颜莎娜 2023-09-12 17:34:08
有一段HTML代码<tr bgcolor="#cceeff" style="page-break-inside:avoid ; font-family:Def.-Times; font-size:8pt"><td colspan="3" valign="top"> <p style=" margin-top:0pt ; margin-bottom:0pt; margin-left:2.00em; text-indent:-1.00em; font-size:8pt; font-family:ARIAL"><b>{99}</b> <b></b>Receivables becoming Defaulted Receivables during period</p></td><td align="right" valign="bottom"><font style="font-family:ARIAL; ">1,310,326.05</font></td> <td nowrap="nowrap" valign="bottom"><font style="font-family:ARIAL; "> </font></td> <td valign="bottom"> </td> <td valign="bottom"></td> <td valign="bottom"></td>我怎样才能提取数据{4}Defaulted Receivables', '{4}', '1,310,326.05']我有一个人给我的代码def get_row(soup, n):    return [td.get_text(strip=True) for td in soup.select('tr:contains("{' + str(n) + '}") td') if td.get_text(strip=True)]但首先,我不明白如何在 HTML 代码中查找“行”。其次, tr:contains("{' + str(n) + '}") td这段代码是做什么的?抱歉,我对抓取和 HTML 还很陌生
查看完整描述

1 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

我改进/更正了代码,现在它将打印每行的条目文本列表列表。

代码需要一次安装 beautiful soup 和 lxml:python -m pip install lxml bs4

在线尝试一下!

# Needs: python -m pip install lxml bs4

import lxml

from bs4 import BeautifulSoup


soup = BeautifulSoup("""

<tr bgcolor="#cceeff" style="page-break-inside:avoid ; font-family:Def.-Times; font-size:8pt">

<td colspan="3" valign="top"> <p style=" margin-top:0pt ; margin-bottom:0pt; margin-left:2.00em; text-indent:-1.00em; font-size:8pt; font-family:ARIAL"><b>{99}</b> <b></b>Receivables becoming Defaulted Receivables during period</p></td>

<td align="right" valign="bottom"><font style="font-family:ARIAL; ">1,310,326.05</font></td>

 <td nowrap="nowrap" valign="bottom"><font style="font-family:ARIAL; "> </font></td>

 <td valign="bottom"> </td>

 <td valign="bottom"></td>

 <td valign="bottom"></td>

</tr>

""", 'lxml')


print([[

    td.get_text(strip=True) for td in tr.select('td') if td.get_text(strip=True)

] for tr in soup.select('tr')])

代码打印:


[['{99}Receivables becoming Defaulted Receivables during period', '1,310,326.05']]


查看完整回答
反对 回复 2023-09-12
  • 1 回答
  • 0 关注
  • 54 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信