2017-07-06 60 views
3

的第一個值我有這樣的標籤: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公司

我向所有人開放的方法。

+0

請發表您當前的代碼在你的問題所以我們知道你需要什麼。否則,你似乎在要求我們爲你寫一個完整的程序,或者你在詢問一兩條特定的線路? –

回答

5

如果你只是想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

print(span.text) 
>>> Actua Corp CIK#: 0001085621 (see all company filings) 
+0

正是我在找什麼,非常感謝。這是最好的答案,我會在5分鐘後給它 – Theo