在HTML表中每个粗体标题后的行中提取特定列使用BeautifulSoup

2024-06-30 07:48:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用美极了。那个粗体文本没有类或id,与它们上面和下面的行处于同一级别。我想我应该用下一个兄弟姐妹,但我不知道该怎么做。在

您可以在这里找到表格的HTML:https://github.com/Tokaalmighty/topmover_table_html/blob/master/html

我的逻辑是:

soup=bs(f1,'html.parser')
topmovers=soup.find('table',{'class':'topmovers'})

bold=topmovers.find_all('b')
gainer=bold[0]
gainer_name=bold.find('tr').next_sibling
gcol1=gainer_name[0]
gcol3=gainer_name[2]

loser=bold[1]
loser_name=bold.find('tr').next_sibling
lcol1=loser_name[0]
lcol3=loser_name[2]

print(gcol1,gcol3,lcol1,lcol3)

Tags: namehtmltablefindtrnextsoupbold
1条回答
网友
1楼 · 发布于 2024-06-30 07:48:09

您可以使用^{}选择下一个'tr',然后用^{}获得文本

soup=bs(f1,'html.parser')
topmovers=soup.find('table',{'class':'topmovers'})

bold=topmovers.find_all('b')
gainer=bold[0]
gainer_name=gainer.find_next('tr')
gainer_strings = list(gainer_name.stripped_strings)
gcol1=gainer_strings[0]
gcol3=gainer_strings[2]

loser=bold[1]
loser_name=loser.find_next('tr')
loser_strings = list(loser_name.stripped_strings)
lcol1=loser_strings[0]
lcol3=loser_strings[2]

print(gcol1, gcol3, lcol1, lcol3)

McDermott International 6.55 Bill Barrett Corporation 2.87

相关问题 更多 >