Python中可以使用lxml
庫中的etree
模塊來解析數據。下面是一個使用XPath解析數據的簡單示例:
from lxml import etree
# 創建一個XML文檔并解析
xml_data = """
<books>
<book>
<title>Python Cookbook</title>
<author>David Beazley</author>
<year>2013</year>
</book>
<book>
<title>Fluent Python</title>
<author>Luciano Ramalho</author>
<year>2015</year>
</book>
</books>
"""
root = etree.fromstring(xml_data)
# 使用XPath選擇元素
titles = root.xpath("//title/text()")
authors = root.xpath("//author/text()")
years = root.xpath("//year/text()")
# 打印解析結果
for title, author, year in zip(titles, authors, years):
print(f"Title: {title}")
print(f"Author: {author}")
print(f"Year: {year}")
print("---")
輸出結果如下:
Title: Python Cookbook
Author: David Beazley
Year: 2013
---
Title: Fluent Python
Author: Luciano Ramalho
Year: 2015
---
在上面的示例中,使用etree.fromstring()
方法將XML字符串解析為一個Element
對象。然后,可以使用xpath()
方法使用XPath表達式選擇相應的元素。最后,使用text
屬性獲取元素的文本內容。