Python 解析簡單的XML數(shù)據(jù)
問題
你想從一個簡單的XML文檔中提取數(shù)據(jù)。
解決方案
可以使用 xml.etree.ElementTree 模塊從簡單的XML文檔中提取數(shù)據(jù)。為了演示,假設(shè)你想解析Planet Python上的RSS源。下面是相應(yīng)的代碼:
from urllib.request import urlopenfrom xml.etree.ElementTree import parse# Download the RSS feed and parse itu = urlopen(’http://planet.python.org/rss20.xml’)doc = parse(u)# Extract and output tags of interestfor item in doc.iterfind(’channel/item’): title = item.findtext(’title’) date = item.findtext(’pubDate’) link = item.findtext(’link’) print(title) print(date) print(link) print()
運行上面的代碼,輸出結(jié)果類似這樣:
Steve Holden: Python for Data AnalysisMon, 19 Nov 2012 02:13:51 +0000http://holdenweb.blogspot.com/2012/11/python-for-data-analysis.html
Vasudev Ram: The Python Data model (for v2 and v3)Sun, 18 Nov 2012 22:06:47 +0000http://jugad2.blogspot.com/2012/11/the-python-data-model.html
Python Diary: Been playing around with Object DatabasesSun, 18 Nov 2012 20:40:29 +0000http://www.pythondiary.com/blog/Nov.18,2012/been-...-object-databases.html
Vasudev Ram: Wakari, Scientific Python in the cloudSun, 18 Nov 2012 20:19:41 +0000http://jugad2.blogspot.com/2012/11/wakari-scientific-python-in-cloud.html
Jesse Jiryu Davis: Toro: synchronization primitives for Tornado coroutinesSun, 18 Nov 2012 20:17:49 +0000http://feedproxy.google.com/~r/EmptysquarePython/~3/_DOZT2Kd0hQ/
很顯然,如果你想做進一步的處理,你需要替換 print() 語句來完成其他有趣的事。
討論
在很多應(yīng)用程序中處理XML編碼格式的數(shù)據(jù)是很常見的。不僅是因為XML在Internet上面已經(jīng)被廣泛應(yīng)用于數(shù)據(jù)交換,同時它也是一種存儲應(yīng)用程序數(shù)據(jù)的常用格式(比如字處理,音樂庫等)。接下來的討論會先假定讀者已經(jīng)對XML基礎(chǔ)比較熟悉了。
在很多情況下,當(dāng)使用XML來僅僅存儲數(shù)據(jù)的時候,對應(yīng)的文檔結(jié)構(gòu)非常緊湊并且直觀。例如,上面例子中的RSS訂閱源類似于下面的格式:
<?xml version='1.0'?><rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1/'> <channel> <title>Planet Python</title> <link>http://planet.python.org/</link> <language>en</language> <description>Planet Python - http://planet.python.org/</description> <item> <title>Steve Holden: Python for Data Analysis</title> <guid>http://holdenweb.blogspot.com/...-data-analysis.html</guid> <link>http://holdenweb.blogspot.com/...-data-analysis.html</link> <description>...</description> <pubDate>Mon, 19 Nov 2012 02:13:51 +0000</pubDate> </item> <item> <title>Vasudev Ram: The Python Data model (for v2 and v3)</title> <guid>http://jugad2.blogspot.com/...-data-model.html</guid> <link>http://jugad2.blogspot.com/...-data-model.html</link> <description>...</description> <pubDate>Sun, 18 Nov 2012 22:06:47 +0000</pubDate> </item> <item> <title>Python Diary: Been playing around with Object Databases</title> <guid>http://www.pythondiary.com/...-object-databases.html</guid> <link>http://www.pythondiary.com/...-object-databases.html</link> <description>...</description> <pubDate>Sun, 18 Nov 2012 20:40:29 +0000</pubDate> </item> ... </channel></rss>
xml.etree.ElementTree.parse() 函數(shù)解析整個XML文檔并將其轉(zhuǎn)換成一個文檔對象。 然后,你就能使用 find() 、iterfind() 和 findtext() 等方法來搜索特定的XML元素了。 這些函數(shù)的參數(shù)就是某個指定的標(biāo)簽名,例如 channel/item 或 title 。 每次指定某個標(biāo)簽時,你需要遍歷整個文檔結(jié)構(gòu)。每次搜索操作會從一個起始元素開始進行。 同樣,每次操作所指定的標(biāo)簽名也是起始元素的相對路徑。 例如,執(zhí)行 doc.iterfind(’channel/item’) 來搜索所有在 channel 元素下面的 item 元素。 doc 代表文檔的最頂層(也就是第一級的 rss 元素)。 然后接下來的調(diào)用 item.findtext() 會從已找到的 item 元素位置開始搜索。 ElementTree 模塊中的每個元素有一些重要的屬性和方法,在解析的時候非常有用。 tag 屬性包含了標(biāo)簽的名字,text 屬性包含了內(nèi)部的文本,而 get() 方法能獲取屬性值。例如:
>>> doc<xml.etree.ElementTree.ElementTree object at 0x101339510>>>> e = doc.find(’channel/title’)>>> e<Element ’title’ at 0x10135b310>>>> e.tag’title’>>> e.text’Planet Python’>>> e.get(’some_attribute’)>>>
有一點要強調(diào)的是 xml.etree.ElementTree 并不是XML解析的唯一方法。對于更高級的應(yīng)用程序,你需要考慮使用 lxml 。它使用了和ElementTree同樣的編程接口,因此上面的例子同樣也適用于lxml。你只需要將剛開始的import語句換成 from lxml.etree import parse 就行了。lxml 完全遵循XML標(biāo)準(zhǔn),并且速度也非???,同時還支持驗證,XSLT和XPath等特性。
以上就是Python 解析簡單的XML數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于Python 解析XML的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 告別AJAX實現(xiàn)無刷新提交表單2. 使用XSL將XML文檔中的CDATA注釋輸出為HTML文本3. IE6/IE7/IE8/IE9中tbody的innerHTML不能賦值的完美解決方案4. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向5. asp讀取xml文件和記數(shù)6. 讀寫xml文件的2個小函數(shù)7. 低版本IE正常運行HTML5+CSS3網(wǎng)站的3種解決方案8. ASP中if語句、select 、while循環(huán)的使用方法9. asp使用Weekday函數(shù)計算項目的結(jié)束時間10. XML入門的常見問題(一)
