当前位置: 首页 > 图灵资讯 > 行业资讯> 一文搞定Xpath简单高效的提取HTML数据

一文搞定Xpath简单高效的提取HTML数据

发布时间:2025-09-26 11:08:29

1.简介

XPath是一门 XML 在文档中找到信息的语言。XPath 用于在 XML 通过元素和属性导航文档。Xpath在提取数据时比BeautifulSoup更方便。

2. 安装

Python中的许多图书馆都有提供Xpath的功能,但最基本的图书馆是lxml,效率最高。在之前的BeautifulSoup章节中,我们还介绍了lxml是如何安装的。

pipinstalllxml

3. 语法

XPath 使用路径表达式 XML 选择文档中的节点。节点沿路径或路径 step 来选取的。

我们将演示以下HTML文档:

html_doc='''<html><head></head><body>
<bookstore>

<bookcategory="COOKING">
<titlelang="en">EverydayItalian</title>
<author>GiadaDeLaurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

<bookcategory="CHILDREN">
<titlelang="en">HarryPotter</title>
<author>JK.Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

<bookcategory="WEB">
<titlelang="en">XQueryKickStart</title>
<author>JamesMcGovern</author>
<author>PerBothner</author>
<author>KurtCagle</author>
<author>JamesLinn</author>
<author>VaidyanathanNagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>

<bookcategory="WEB">
<titlelang="en">LearningXML</title>
<author>ErikT.Ray</author>
<year>2003</year>
<price>39.95</price>
</book>

</bookstore></body></html>'''

导入语句,生成HTMLDOM树:

fromlxmlimportetree

page=etree.HTML(html_doc)

3.1. 路径查找

defd07f9ada5ba5d4f5495712eb2fcd.png

找到当前节点的子节点

In[1]:page.xpath('head')
Out[1]:[<elementheadat0x11c74c4>]

从根节点搜索

In[2]:page.xpath('/html')
Out[2]:[<elementhtmlat0x1208>]

查找整个文档中的所有节点

In[3]:page.xpath('//book')
Out[3]:
[<Elementbookat0x128c02c00>,<Elementbookat0x11c74>,<elementbookat0x11fd2288>,<Elementbookat
0x1128da348>]

选择当前节点的父节点

In[4]:page.xpath('//book')[0].xpath('..')
Out[4]:[<elementbookstoreat0x128c0ac8>]

选取属性

In[5]:page.xpath('//book')[0].xpath('@category')
Out[5]:['COOKING']

3.2. 节点查找

07f5eaac231e8411ebe2c461dc2e62c.png

nodename[1]

选择第一个元素。

nodename[last()]

选择最后一个元素。

nodename[last()-1]

选择倒数第二个元素。

nodename[position()<3]

选择前两个子元素。

nodename[@lang]

选择拥有的名字 lang 属性元素。

nodename[@lang='eng']

具有lang属性的选择,值为 eng 的元素。

选择第二个book元素

In[1]:page.xpath('//book[2]/@category')
Out[1]:['CHILDREN']

选择倒数第三的book元素

In[2]:page.xpath('//book[last()-2]/@category')
Out[2]:['CHILDREN']

从第二个元素开始选择所有元素

In[3]:page.xpath('//book[position()>1]/@category')
Out[3]:['CHILDREN','WEB','WEB']

为WEB选择category属性的元素

In[4]:page.xpath('//book[@category="WEB"]/@category')
Out[4]:['WEB','WEB']

3.3. 未知节点

f3bec962b40697e607f4f396dbf4af7.png

在第一个book元素下匹配所有元素

In[1]:page.xpath('//book[1]/*')
Out[1]:
[<elementtitleat0x11f78>,
<elentauthorat0x11f78>,
<Elementyearat0x1128c1a88>,
<elementpriceat0x128cc8>]

3.4. 在节点中获取文本

在某个节点下使用text()获取文本

In[1]:page.xpath('//book[1]/author/text()')
Out[1]:['GiadaDeLaurentiis']

若该节点下有多个文本,则只能获得一段。

在某个节点下,使用string()获取所有文本

In[2]:page.xpath('string(//book[1])')
Out[2]:'\nEverydayItalian\nGiadaDeLaurentiis\n

3.5. 选择多条路径

通过在路径表达式中使用“|”运算符,可以选择几条路径。

In[1]:page.xpath('//book[1]/title/text()|//book[1]/author/text()')
Out[1]:['EverydayItalian','GiadaDeLaurentiis']

相关文章

Python中reduce函数和lambda表达式的学习

Python中reduce函数和lambda表达式的学习

2025-09-26
Python小白必学的面向对象

Python小白必学的面向对象

2025-09-26
一个例子解释python装饰器

一个例子解释python装饰器

2025-09-26
深入理解Python的set和dict

深入理解Python的set和dict

2025-09-26
Python中正则表达式的巧妙使用

Python中正则表达式的巧妙使用

2025-09-26
5分钟搞定Python中函数的参数

5分钟搞定Python中函数的参数

2025-09-26