我試圖用BeautifulSoup解析某些XML,看起來像BeautifulStoneSoup AttributeError的:「NavigableString」對象有沒有屬性「子標籤」
<a>
<b>
<c>
<d attr="x">
<e>
</e>
<name>
</name>
</d>
</c>
<c>
...
<c>
</b>
</a>
,但我無法弄清楚如何訪問e
或name
在一個循環中。這工作:
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
屬性。有更好的方法嗎?
這是什麼解析的目標是什麼?你是否想要提取第一個'a.b.c'元素中包含的每個'd'元素的第一個'e'元素? – 2011-04-24 00:03:46
我想要所有('d','e','name')組。 – wes 2011-04-24 00:10:56