2012-03-06 54 views
1

我有這個代碼正在工作,但我怎樣才能加載項目值而不是對象本身,並將其記錄到數組中。使用Python加載XML數據

import urllib 
import xml.etree.ElementTree as xml 
tree = xml.parse(urllib.urlopen('http://lazhalazha.livejournal.com/data/rss')) 
rootElement = tree.getroot() 
for a in rootElement.findall('channel/item/guid'): 
    print a 

輸出

<Element 'guid' at 0xb75316ec> 
<Element 'guid' at 0xb7531a0c> 
<Element 'guid' at 0xb7531c8c> 
<Element 'guid' at 0xb7531f0c> 
<Element 'guid' at 0xb753a22c> 
<Element 'guid' at 0xb753a4ec> 
<Element 'guid' at 0xb753a7ac> 
<Element 'guid' at 0xb753aa6c> 
<Element 'guid' at 0xb753ad2c> 
+1

'urllib'是不必要的。 'xml.parse(theurl)'可以正常工作。 – 2012-03-06 18:55:20

回答

1

也許你想a.text

如果​​元素可能包含帶有自己文本的子元素,並且想要整個文本的文本序列化,請使用etree.tostring(a, method='text')

這是一個非常基本的問題。請通讀Element課上的lxml tutorial