2017-04-20 115 views
0

我使用BeautifulSoup解析通過html並試圖檢索標題。BeautifulSoup獲得標題返回'NoneType'對象沒有屬性'__getitem__'

我的代碼如下:

callerid = cell_list[0] 
print callerid.find('a') 

,返回的我想從我的提取「標題」適當的錨標記。

<a class="caller_ref" href="/tomasi/cardio/vgh/SPsdeGBHH" 
title="CDS1255S56d">identifier</a> 

現在,這裏是它變得時髦。當我加入[「標題」]我的print語句來提取標題,

callerid = cell_list[0] 
print callerid.find('a')["title"] 

我得到

AttributeError: 'NoneType' object has no attribute 'find'

這怎麼能是「NoneType」時,它顯然包含了錨標記HTML如第一個例子所示,我如何解析它以返回標題?

+0

你可以在'callerid = cell_list [0]'後面加上'print callerid'並顯示結果嗎? – kvorobiev

+0

添加'[「標題」]'不會導致此錯誤。錯誤發生是因爲'callerid'是'None'。 – kindall

+0

@kvorobiev是肯定的結果是 ' identifier' –

回答

0

callerid.find('a')應該是callerid.find('a').a['title']
它可能看起來像它,但callerid.find('a')實際上並不返回標籤的內容! (其實the documentation並不至於什麼它確實回報非常翔實的...?)

0

嘗試,

from bs4 import BeautifulSoup 
content = '<a class="caller_ref" href="/tomasi/cardio/vgh/SPsdeGBHH" 
title="CDS1255S56d">identifier</a>' 
soup = BeautifulSoup(content) 
anchor = soup.find_all('a')[0] 
print "title : " + (anchor.get('title')) 
0

我趕上了錯,我基本上是通過表解析與多個行,所有行都有錨標記,因此print callerid.find('a')可以工作。

但是至於print callerid.find('a')["title"]此行將返回NoneType,因爲表i的第一行解析是唯一一行(19456行之外)沒有標題標記,這會停止所有進一步的執行。

謝謝大家的幫助。

相關問題