W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
你想從一個簡單的XML文檔中提取數(shù)據。
可以使用 xml.etree.ElementTree
模塊從簡單的XML文檔中提取數(shù)據。為了演示,假設你想解析Planet Python上的RSS源。下面是相應的代碼:
from urllib.request import urlopen
from xml.etree.ElementTree import parse
# Download the RSS feed and parse it
u = urlopen('http://planet.python.org/rss20.xml')
doc = parse(u)
# Extract and output tags of interest
for 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()
運行上面的代碼,輸出結果類似這樣:
Steve Holden: Python for Data Analysis
Mon, 19 Nov 2012 02:13:51 +0000
http://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 +0000
http://jugad2.blogspot.com/2012/11/the-python-data-model.html
Python Diary: Been playing around with Object Databases
Sun, 18 Nov 2012 20:40:29 +0000
http://www.pythondiary.com/blog/Nov.18,2012/been-...-object-databases.html
Vasudev Ram: Wakari, Scientific Python in the cloud
Sun, 18 Nov 2012 20:19:41 +0000
http://jugad2.blogspot.com/2012/11/wakari-scientific-python-in-cloud.html
Jesse Jiryu Davis: Toro: synchronization primitives for Tornado coroutines
Sun, 18 Nov 2012 20:17:49 +0000
http://feedproxy.google.com/~r/EmptysquarePython/~3/_DOZT2Kd0hQ/
很顯然,如果你想做進一步的處理,你需要替換 print()
語句來完成其他有趣的事。
在很多應用程序中處理XML編碼格式的數(shù)據是很常見的。不僅是因為XML在Internet上面已經被廣泛應用于數(shù)據交換,同時它也是一種存儲應用程序數(shù)據的常用格式(比如字處理,音樂庫等)。接下來的討論會先假定讀者已經對XML基礎比較熟悉了。
在很多情況下,當使用XML來僅僅存儲數(shù)據的時候,對應的文檔結構非常緊湊并且直觀。例如,上面例子中的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文檔并將其轉換成一個文檔對象。 然后,你就能使用 find()
、iterfind()
和 findtext()
等方法來搜索特定的XML元素了。 這些函數(shù)的參數(shù)就是某個指定的標簽名,例如 channel/item
或 title
。 每次指定某個標簽時,你需要遍歷整個文檔結構。每次搜索操作會從一個起始元素開始進行。 同樣,每次操作所指定的標簽名也是起始元素的相對路徑。 例如,執(zhí)行 doc.iterfind('channel/item')
來搜索所有在 channel
元素下面的 item
元素。 doc
代表文檔的最頂層(也就是第一級的 rss
元素)。 然后接下來的調用 item.findtext()
會從已找到的 item
元素位置開始搜索。 ElementTree
模塊中的每個元素有一些重要的屬性和方法,在解析的時候非常有用。 tag
屬性包含了標簽的名字,text
屬性包含了內部的文本,而 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')
>>>
有一點要強調的是 xml.etree.ElementTree
并不是XML解析的唯一方法。對于更高級的應用程序,你需要考慮使用 lxml
。它使用了和ElementTree同樣的編程接口,因此上面的例子同樣也適用于lxml。你只需要將剛開始的import語句換成 from lxml.etree import parse
就行了。lxml
完全遵循XML標準,并且速度也非???,同時還支持驗證,XSLT和XPath等特性。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: