2011-04-23 25 views
2

我試圖用BeautifulSoup解析某些XML,看起來像BeautifulStoneSoup AttributeError的:「NavigableString」對象有沒有屬性「子標籤」

<a> 
    <b> 
    <c> 
     <d attr="x"> 
     <e> 
     </e> 
     <name> 
     </name> 
     </d> 
    </c> 
    <c> 
     ... 
    <c> 
    </b> 
</a> 

,但我無法弄清楚如何訪問ename在一個循環中。這工作:

print soup.a.b.c.d.e 

但這並不:

for subtag in soup.a.b.c: 
    print subtag.d.e 

而是給出了這樣的錯誤:

AttributeError: 'NavigableString' object has no attribute 'd' 

,有點unrelatedly,這樣的:

print soup.a.b.c.d.name 

輸出d

我在做什麼錯?我懷疑對於第二個問題,我將不得不使用find(),而不是因爲該對象已具有name屬性。有更好的方法嗎?

+0

這是什麼解析的目標是什麼?你是否想要提取第一個'a.b.c'元素中包含的每個'd'元素的第一個'e'元素? – 2011-04-24 00:03:46

+0

我想要所有('d','e','name')組。 – wes 2011-04-24 00:10:56

回答

1

因爲BSS returns NavigableStrings,當您循環使用Tag實例時,會得到一個AttributeError。也許你也想嘗試:

soup.a.b.c.findChildren() 
#[<d attr="x"> 
#<e> 
#</e> 
#<name> 
#</name> 
#</d>, <e> 
#</e>, <name> 
#</name>] 

關於這個問題瓦特/ name:它是specified as an attribute,但可以改爲做:

soup.a.b.c.d.findChildren('name') 
#[<name> 
#</name>] 

設置代碼以供參考:

from BeautifulSoup import BeautifulStoneSoup as bss 
soup = bss(markup) 
1

這個:

print soup.a.b.c.d.name 

只有輸出d。

這是因爲name與Tag對象的內置name屬性衝突。根據using tags names as members的文檔,您可以改爲使用soup.a.b.c.d.nameTag

AttributeError在其他答案中有很好的解釋。如果你想每一個(d, e, name)整個文檔中三元,提取不管出現d標籤在那裏,你可以做這樣的事情:

soup = BeautifulStoneSoup(doc) 
for d in soup.findAll('d'): 
    print (d, d.e, d.nameTag) 
+0

「nameTag」的+1 – bernie 2011-04-24 00:29:04

相關問題