有一段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还是很陌生
答案 0 :(得分:1)
我已经改进/更正了代码,现在它将为每一行打印条目文本列表。
代码需要像python -m pip install lxml bs4
一样安装漂亮的汤和lxml。
# 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']]