Python学习:python扩展库lxml学习

本文介绍如何使用lxml库结合XPath语法操作XML文件,包括解析、查找、修改、添加及删除节点等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这几天一直想找到一个xml库,能够方便搜索,搜索条件能够满足xpath语法规则的。网上有很多xml库,在python下,找到lxml扩展库比较方便。虽然需要手工安装,但lxml却是很方便。windows下安装又傻瓜化的。把一些学习过程中摸索的记录下来,便于后续自己查阅,网上搜到的例子,实在是过于简单,也不适用,我主要查找xml节点,然后修改再保存。

首先准备一个xml文件,xml文件的内容,就用网上到处都有的xml例子。文件内容如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
	​<book>
    	<title lang="eng">Harry Potter</title>
    	<price>29.99</price>
    	</book>
    <book>
    	<title lang="eng">Learning XML</title>
    	<price>39.95</price>
    	</book>
</bookstore>

1、解析文件

使用etree的parse函数,直接解析xml文件。测试的时候,也可以使用文本形式,不从文件读,就得用etree的fromstring函数。

from lxml import etree

store = etree.parse("/code/python/store.xml")
print(etree.tostring(store))

输出:

b'<bookstore>\n\t<book>\n    \t<title lang="eng">Harry Potter</title>\n    \t<price>29.99</price>\n    \t</book>\n    <book>\n    \t<title lang="eng">Learning XML</title>\n    \t<price>39.95</price>\n    \t</book>\n</bookstore>'


2、使用xpath查找所有book节点

可以使用下面这个代码。xpath返回的是一个list,可以for打印每个元素。如果找不到,返回的list是空的

book_list = store.xpath("//book")
for item in book_list:
    print(etree.tostring(item))
输出:

b'<book>\n    \t<title lang="eng">Harry Potter</title>\n    \t<price>29.99</price>\n    \t</book>\n    '

b'<book>\n    \t<title lang="eng">Learning XML</title>\n    \t<price>39.95</price>\n    \t</book>\n'


3、使用xpath查找节点title的内容为Learning XML

price = store.xpath("/bookstore/book[title='Learning XML']/price")
print(price[0].text)

输出:

39.95


4、使用xpath查找title节点树形lang='eng'的节点信息。通过getnext函数,返回该节点下一个节点信息。

price = store.xpath("/bookstore/book/title[@lang='eng']")
print(price[0].text)
print(price[0].getnext().text)

输出:

Harry Potter
29.99

5、使用xpath查找属性值

lang = store.xpath("/bookstore/book/title[@lang='eng']/@lang")
print(lang)

输出:

['eng', 'eng']

6、设置和添加属性和值

title = store.xpath("/bookstore/book/title[@lang='eng']")
print(etree.tostring(title[0]))
title[0].set('newattr', 'this is new attr value')
print(etree.tostring(title[0]))
title[0].set('newattr', 'modify attr value')
print(etree.tostring(title[0]))


输出:

b'<title lang="eng">Harry Potter</title>\n \t'
b'<title lang="eng" newattr="this is new attr value">Harry Potter</title>\n \t'
b'<title lang="eng" newattr="modify attr value">Harry Potter</title>\n \t'


7、设置节点值

title = store.xpath("/bookstore/book/title[@lang='eng']")
print(etree.tostring(title[0]))
title[0].text = 'Harry Potter New Name'
print(etree.tostring(title[0]))

输出:

b'<title lang="eng" newattr="modify attr value">Harry Potter</title>\n \t'
b'<title lang="eng" newattr="modify attr value">Harry Potter New Name</title>\n \t'


8、删除节点

title = store.xpath("/bookstore/book/title[@lang='eng']")
parent = title[0].getparent()
print(etree.tostring(parent))
parent.remove(title[0])
print(etree.tostring(parent))


输出:

b'<book>\n\t\t<title lang="eng">Harry Potter</title>\n \t<price>29.99</price>\n \t</book>\n '
b'<book>\n\t\t<price>29.99</price>\n \t</book>\n '


9、保存文件

store = etree.parse("/code/python/store.xml")
print(etree.tostring(store))
title = store.xpath("/bookstore/book/title[@lang='eng']")
print(etree.tostring(title[0]))
title[0].text = 'Harry Potter New Name'
print(etree.tostring(title[0]))
store.write('/code/python/store_new.xml', encoding='GBK')
store = etree.parse("/code/python/store_new.xml")
print(etree.tostring(store))

输出:

b'<bookstore>\n\t<book>\n\t\t<title lang="eng">Harry Potter</title>\n \t<price>29.99</price>\n \t</book>\n <book>\n \t<title lang="eng">Learning XML</title>\n \t<price>39.95</price>\n \t</book>\n</bookstore>'
b'<title lang="eng">Harry Potter</title>\n \t'
b'<title lang="eng">Harry Potter New Name</title>\n \t'
b'<bookstore>\n\t<book>\n\t\t<title lang="eng">Harry Potter New Name</title>\n \t<price>29.99</price>\n \t</book>\n <book>\n \t<title lang="eng">Learning XML</title>\n \t<price>39.95</price>\n \t</book>\n</bookstore>'

未完待续


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值