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()
運(yùn)行上面的代碼,輸出結(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/
很顯然,如果你想做進(jìn)一步的處理,你需要替換 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)。每次搜索操作會從一個起始元素開始進(jìn)行。 同樣,每次操作所指定的標(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’)>>>
有一點要強(qiáng)調(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. php使用正則驗證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)2. XML在語音合成中的應(yīng)用3. 基于javaweb+jsp實現(xiàn)企業(yè)車輛管理系統(tǒng)4. asp.net core 認(rèn)證和授權(quán)實例詳解5. 基于PHP做個圖片防盜鏈6. ASP.NET MVC使用Boostrap實現(xiàn)產(chǎn)品展示、查詢、排序、分頁7. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)8. Jsp+Servlet實現(xiàn)文件上傳下載 文件列表展示(二)9. ASP.NET MVC把數(shù)據(jù)庫中枚舉項的數(shù)字轉(zhuǎn)換成文字10. jscript與vbscript 操作XML元素屬性的代碼
