Python 解析簡(jiǎn)單的XML數(shù)據(jù)
問(wèn)題
你想從一個(gè)簡(jiǎn)單的XML文檔中提取數(shù)據(jù)。
解決方案
可以使用 xml.etree.ElementTree 模塊從簡(jiǎn)單的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ǔ)句來(lái)完成其他有趣的事。
討論
在很多應(yīng)用程序中處理XML編碼格式的數(shù)據(jù)是很常見(jiàn)的。不僅是因?yàn)閄ML在Internet上面已經(jīng)被廣泛應(yīng)用于數(shù)據(jù)交換,同時(shí)它也是一種存儲(chǔ)應(yīng)用程序數(shù)據(jù)的常用格式(比如字處理,音樂(lè)庫(kù)等)。接下來(lái)的討論會(huì)先假定讀者已經(jīng)對(duì)XML基礎(chǔ)比較熟悉了。
在很多情況下,當(dāng)使用XML來(lái)僅僅存儲(chǔ)數(shù)據(jù)的時(shí)候,對(duì)應(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ù)解析整個(gè)XML文檔并將其轉(zhuǎn)換成一個(gè)文檔對(duì)象。 然后,你就能使用 find() 、iterfind() 和 findtext() 等方法來(lái)搜索特定的XML元素了。 這些函數(shù)的參數(shù)就是某個(gè)指定的標(biāo)簽名,例如 channel/item 或 title 。 每次指定某個(gè)標(biāo)簽時(shí),你需要遍歷整個(gè)文檔結(jié)構(gòu)。每次搜索操作會(huì)從一個(gè)起始元素開(kāi)始進(jìn)行。 同樣,每次操作所指定的標(biāo)簽名也是起始元素的相對(duì)路徑。 例如,執(zhí)行 doc.iterfind(’channel/item’) 來(lái)搜索所有在 channel 元素下面的 item 元素。 doc 代表文檔的最頂層(也就是第一級(jí)的 rss 元素)。 然后接下來(lái)的調(diào)用 item.findtext() 會(huì)從已找到的 item 元素位置開(kāi)始搜索。 ElementTree 模塊中的每個(gè)元素有一些重要的屬性和方法,在解析的時(shí)候非常有用。 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ǎn)要強(qiáng)調(diào)的是 xml.etree.ElementTree 并不是XML解析的唯一方法。對(duì)于更高級(jí)的應(yīng)用程序,你需要考慮使用 lxml 。它使用了和ElementTree同樣的編程接口,因此上面的例子同樣也適用于lxml。你只需要將剛開(kāi)始的import語(yǔ)句換成 from lxml.etree import parse 就行了。lxml 完全遵循XML標(biāo)準(zhǔn),并且速度也非常快,同時(shí)還支持驗(yàn)證,XSLT和XPath等特性。
以上就是Python 解析簡(jiǎn)單的XML數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于Python 解析XML的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 使用css實(shí)現(xiàn)全兼容tooltip提示框2. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法3. 告別AJAX實(shí)現(xiàn)無(wú)刷新提交表單4. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)5. SharePoint Server 2019新特性介紹6. XML入門(mén)的常見(jiàn)問(wèn)題(一)7. 詳解盒子端CSS動(dòng)畫(huà)性能提升8. Vue+elementUI下拉框自定義顏色選擇器方式9. ASP中常用的22個(gè)FSO文件操作函數(shù)整理10. XML入門(mén)的常見(jiàn)問(wèn)題(四)
