Python靓汤,如何得到第一个值的标签

2024-09-26 18:06:12 发布

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

我有这个标签:

<span class="companyName">Actua Corp <acronym title="Central Index Key">CIK</acronym>#: <a href="/cgi-bin/browse-edgar?action=getcompany&amp;CIK=0001085621&amp;owner=include&amp;count=40">0001085621 (see all company filings)</a></span>

我如何获得<span class="companyName">之后的值。在

这里是Actua公司

我对任何方法都持开放态度。在


Tags: keyindextitle标签classamphrefspan
1条回答
网友
1楼 · 发布于 2024-09-26 18:06:12

如果您只想Actua Corp,可以使用next

r = '<span class="companyName">Actua Corp <acronym title="Central Index Key">CIK</acronym>#: <a href="/cgi-bin/browse-edgar?action=getcompany&amp;CIK=0001085621&amp;owner=include&amp;count=40">0001085621 (see all company filings)</a></span>'

from bs4 import BeautifulSoup    
soup = BeautifulSoup(r)

span = soup.find('span', {'class': 'companyName'})
print(span.next)
>>> Actua Corp

如果您想要span中的所有文本,可以使用text

^{pr2}$

相关问题 更多 >

    热门问题