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)
相关推荐
AllData公司负责人17 分钟前
通过Postgresql同步到Doris,全视角演示AllData数据中台核心功能效果,涵盖:数据入湖仓,数据同步,数据处理,数据服务,BI可视化驾驶舱
java·大数据·数据库·数据仓库·人工智能·python·postgresql
Flittly1 小时前
【LangGraph新手村系列】(5)时间旅行:浏览历史、分叉时间线与修改过去
python·langchain
2301_782040452 小时前
CSS Flex布局中如何实现导航栏与Logo的左右分布_利用justify-content- space-between
jvm·数据库·python
yaoxin5211232 小时前
400. Java 文件操作基础 - 使用 Buffered Stream I/O 读取文本文件
java·开发语言·python
用户8356290780513 小时前
使用 Python 自动创建 Excel 折线图
后端·python
小白学大数据3 小时前
面向大规模爬取:Python 全站链接爬虫优化(过滤 + 断点续爬)
开发语言·爬虫·python
WL_Aurora4 小时前
【每日一题】贪心
python·算法
IT策士4 小时前
Python 中间件系列:redis 深入浅出
redis·python·中间件
Dxy12393102164 小时前
Python Pillow库:`img.format`与`img.mode`的区别详解
开发语言·python·pillow
༒࿈南林࿈༒5 小时前
刺猬猫小说下载
python·js逆向