XPath与lxml解析库

test.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>

<bookstore>

    <book name="halibote">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
        <abc>
            <book lang="中文">neibu</book>
        </abc>
    </book>

    <book name="hongloumeng">
        红楼梦
    </book>

</bookstore>

hello.html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!-- hello.html -->
<div>
    <ul>
        <li class="item-0">meiguo<a href="link1.html">first item</a></li>
        <li class="item-1"><a href="link2.html">second item</a></li>
        <li class="item-inactive"><a href="link3.html"><span
                class="bold">third item</span></a></li>
        <li class="item-1"><a href="link4.html">fourth item</a></li>
        <li class="item-0"><a href="link5.html">fifth item</a></li>
    </ul>
</div>

</body>
</html>

选取节点

python 复制代码
from lxml import etree

tree = etree.parse("test.xml")
list_node = tree.xpath("book/@name")
print(list_node[0])

list_node = tree.xpath("/bookstore")
print(list_node[0])
list_node = tree.xpath("book/title")
print(list_node[0].text)
list_node = tree.xpath("book//book")
print(list_node)
list_node = tree.xpath("//@lang")
print(list_node)

谓语:指路径表达式的附加条件

python 复制代码
from lxml import etree

tree = etree.parse("test.xml")
list_node = tree.xpath("book[2]")
print(list_node[0].text)

选取未知节点

python 复制代码
from lxml import etree

tree = etree.parse("test.xml")
list_node = tree.xpath("/bookstore/*")
print(list_node)

选取若干路径

python 复制代码
from lxml import etree

tree = etree.parse("test.xml")
list_node = tree.xpath("//book/title | //book/price")
print(list_node)

通过轴限定

python 复制代码
from lxml import etree

tree = etree.parse("test.xml")
list_node = tree.xpath("descendant::book")
print(list_node)

操作XML节点

python 复制代码
from lxml import etree

root = etree.Element("root",a="1")
child = etree.SubElement(root, "child")
root.set("b", "2")
root.text = "yilang"
print(etree.tostring(root))
print(root.tag)

print(root.text)

# 从字符串中解析XML,返回根节点
root = etree.XML("<root>"
                    "<a x='123'>aText"
                        "<b/>"
                        "<c/>"
                        "<b/>"
                    "</a>"
                 "</root>")
# 从根节点查找,返回匹配到的节点名称
print(root.find("a").tag)
# 从根节点开始查找,返回匹配到的第一个节点的名称
print(root.findall(".//a[@x]")[0].tag)

在XML中搜索

python 复制代码
from lxml import etree

tree = etree.parse("hello.html",parser=etree.HTMLParser())
list_node = tree.xpath("//li")
print(list_node[0].text)
相关推荐
彦为君7 小时前
JavaSE-07-异常机制
java·开发语言·后端·python·spring
适应规律7 小时前
【无标题】
人工智能·python·算法
XLYcmy7 小时前
全链路验证测试系统:一个针对智能代理(Agent)系统全链路能力的自动化验证脚本
分布式·python·http·网络安全·ai·llm·agent
有味道的男人7 小时前
电商效率翻倍:京东全量商品信息抓取
python
原来是猿8 小时前
博客系统自动化测试实战总结
python
小江的记录本8 小时前
【JVM虚拟机】JVM调优:常用JVM参数、调优核心指标、OOM排查、GC日志分析、Arthas工具使用(附《思维导图》+《面试高频考点清单》)
java·jvm·spring boot·后端·python·spring·面试
大数据魔法师8 小时前
Streamlit(十三)- API 参考文档(六)- 媒体展示组件
python·web
爱写代码的倒霉蛋8 小时前
Hello-Agents的第一个练习-5分钟实现一个智能体(实现详解)
python
金銀銅鐵8 小时前
[Java] 用图形化界面演示 iadd, isub, iconst_<i> 指令的效果
java·后端·python