2012-06-25 43 views
3

我試圖在這個代碼的網頁中找到類「數據」的表。如何使用BeautifulSoup在網頁中查找具有特定類的元素?

import urllib2 
from BeautifulSoup import BeautifulSoup 

soup = BeautifulSoup(urllib2.urlopen('http://www.cbssports.com/nba/draft/mock-draft').read()) 

rows = soup.findAll("table.data") 
print rows 

但是,即使我確定該頁面上存在具有類「數據」的表格,我也沒有收到任何行。使用BeautifulSoup在網頁上查找類「數據」元素的正確方法是什麼?

+0

所以你試圖分析出他們的選秀權模擬草案?或者完全做其他事情? –

回答

2

如果你想拿起行,你需要以下

import urllib2 
from BeautifuSoup import BeautifulSoup 

soup = BeautifulSoup(urllib2.urlopen('http://www.cbssports.com/nba/draft/mock-draft').read()) 

# if there's only one table with class = data 
table = soup.find('table', attrs = {'class' : 'data'}) 

# if there are multiple tables with class = data 
table = soup.findAll('table', attrs = {'class' : 'data'})[n] 
# suppose you need the n-th table of the list returned 

rows = table.findAll('tr') # gives all the rows, you can set attrs to filter 

那麼你也可以通過列迭代:

for row in rows: 
    cols = row.findAll('td') 
    ... 
0

你想要的東西像

rows = soup.find_all('table', attrs = {"class": "data"}) 

,代替目前的線(測試)。元素的類是一個屬性,因此您可以通過find_all中的屬性進行過濾。該行從示例頁面返回一個大表格元素。

+1

它只是沒有工作,其empy列表再次 –

+0

我不知道該說什麼 - 該代碼適用於我的電腦。唯一的區別是我使用「從bs4導入BeautifulSoup」而不是問題中的導入行。除此之外,當我運行我的腳本時,'rows'變量具有table元素。 – Ansari

+0

這是它彈出的錯誤TypeError:'NoneType'對象不可調用 –

相關問題