這是我的第一個BS編碼實驗。
網站我刮是here
我遇到的問題是這段代碼:<span>之前的文本標記
<div class="cmg_team_name">
ARST
<span>101</span>
</div>
這裏是我的代碼:
import numpy as np
from bs4 import BeautifulSoup
import urllib2
url = "http://covers.com/Sports/NCAAF/Matchups?selectedDate=2015-10-13"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read(), 'html.parser')
universities = soup.find_all('div',{'class':'cmg_team_name'})
#for university in universities:
# print university.contents
# print "****************"
print universities[0]
我的問題是我怎麼只能訪問span標誌之前的文本。在這個例子中,我只想要ARST
。 我試過print universities[0].get_text()
,print universities[0].string
得到None
,也是數組符號。我知道我將如何在C++中做到這一點,但我假設python/BSoup會在一行中做到這一點...感謝!
請不要更新您的代碼以包含解決方案,因爲它會使答案無效。 – ChrisF