我有这个标签:
<span class="companyName">Actua Corp <acronym title="Central Index Key">CIK</acronym>#: <a href="/cgi-bin/browse-edgar?action=getcompany&CIK=0001085621&owner=include&count=40">0001085621 (see all company filings)</a></span>
我将如何获得之后的值 <span class="companyName">
.
在这种情况下是 Actua Corp.
我对所有方法都持开放态度。
最佳答案
如果你只是想要Actua Corp
,你可以使用next
r = '<span class="companyName">Actua Corp <acronym title="Central Index Key">CIK</acronym>#: <a href="/cgi-bin/browse-edgar?action=getcompany&CIK=0001085621&owner=include&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
print(span.text)
>>> Actua Corp CIK#: 0001085621 (see all company filings)
关于Python - Beautiful Soup,如何获取标签的第一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44957121/