猿问

从 edgar 中抓取特定数据

有一段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 还很陌生


红颜莎娜
浏览 97回答 1
1回答

慕尼黑的夜晚无繁华

我改进/更正了代码,现在它将打印每行的条目文本列表列表。代码需要一次安装 beautiful soup 和 lxml:python -m pip install lxml bs4。在线尝试一下!# Needs: python -m pip install lxml bs4import lxmlfrom bs4 import BeautifulSoupsoup = 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>&nbsp;<td nowrap="nowrap" valign="bottom"><font style="font-family:ARIAL; "> </font></td>&nbsp;<td valign="bottom"> </td>&nbsp;<td valign="bottom"></td>&nbsp;<td valign="bottom"></td></tr>""", 'lxml')print([[&nbsp; &nbsp; 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']]
随时随地看视频慕课网APP

相关分类

Python
我要回答