xPath 用法总结整理
xPath 用法总结整理
一、xpath介绍
XPath 是一门在 XML 文档中查找信息的语言。XPath 用于在 XML 文档中通过元素和属性进行导航。
- XPath 使用路径表达式在 XML 文档中进行导航
- XPath 包含一个标准函数库
- XPath 是 XSLT 中的主要元素
- XPath 是一个 W3C 标准
节点
在 XPath 中,有七种类型的节点:元素、属性、文本、命名空间、处理指令、注释以及文档(根)节点。XML 文档是被作为节点树来对待的。
二、xpath语法
表达式 | 描述 |
---|---|
nodename | 选取此节点的所有子节点。 |
/ | 从根节点选取。 |
// | 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。 |
. | 选取当前节点。 |
.. | 选取当前节点的父节点。 |
@ | 选取属性。 |
例子
以下面这个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>
<?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>
- xml.xpath(“bookstore”) 表示选取 bookstore 元素的所有子节点
- xml.xpath(“/bookstore”) 表示选取根元素 bookstore。
- xml.xpath(“bookstore/book”) 选取属于 bookstore 的子元素的所有 book 元素。
- xml.xpath(“//book”) 选取所有 book 子元素,而不管它们在文档中的位置。
- xml.xpath(“bookstore//book”) 选择属于 bookstore 元素的后代的所有 book 元素,而不管它们位于 bookstore 之下的什么位置。
- xml.xpath(“//@lang”) 选取名为 lang 的所有属性。
谓语
路径表达式 | 结果 |
---|---|
/bookstore/book[1] | 选取属于 bookstore 子元素的第一个 book 元素。 |
/bookstore/book[last()] | 选取属于 bookstore 子元素的最后一个 book 元素。 |
/bookstore/book[last()-1] | 选取属于 bookstore 子元素的倒数第二个 book 元素。 |
/bookstore/book[position()<3] | 选取最前面的两个属于 bookstore 元素的子元素的 book 元素。 |
//title[@lang] | 选取所有拥有名为 lang 的属性的 title 元素。 |
//title[@lang=’eng’] | 选取所有 title 元素,且这些元素拥有值为 eng 的 lang 属性。 |
/bookstore/book[price>35.00] | 选取 bookstore 元素的所有 book 元素,且其中的 price 元素的值须大于 35.00。 |
/bookstore/book[price>35.00]/title | 选取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值须大于 35.00。 |
选取未知节点
通配符 | 描述 |
---|---|
* | 匹配任何元素节点。 |
@* | 匹配任何属性节点。 |
node() | 匹配任何类型的节点。 |
例子:
路径表达式 | 结果 |
---|---|
/bookstore/* | 选取 bookstore 元素的所有子元素。 |
//* | 选取文档中的所有元素。 |
//title[@*] | 选取所有带有属性的 title 元素。 |
选取若干路径
通过在路径表达式中使用“|”运算符,您可以选取若干个路径。
//book/title | //book/price
选取 book 元素的所有 title 和 price 元素。//title | //price
选取文档中的所有 title 和 price 元素。/bookstore/book/title | //price
选取属于 bookstore 元素的 book 元素的所有 title 元素,以及文档中所有的 price 元素。
三、轴
轴可定义相对于当前节点的节点集。
轴名称 | 结果 |
---|---|
ancestor | 选取当前节点的所有先辈(父、祖父等)。 |
ancestor-or-self | 选取当前节点的所有先辈(父、祖父等)以及当前节点本身。 |
attribute | 选取当前节点的所有属性。 |
child | 选取当前节点的所有子元素。 |
descendant | 选取当前节点的所有后代元素(子、孙等)。 |
descendant-or-self | 选取当前节点的所有后代元素(子、孙等)以及当前节点本身。 |
following | 选取文档中当前节点的结束标签之后的所有节点。 |
namespace | 选取当前节点的所有命名空间节点。 |
parent | 选取当前节点的父节点。 |
preceding | 选取文档中当前节点的开始标签之前的所有节点。 |
preceding-sibling | 选取当前节点之前的所有同级节点。 |
self | 选取当前节点。 |
步的语法: 轴名称::节点测试[谓语]
例子:
例子 | 结果 |
---|---|
child::book | 选取所有属于当前节点的子元素的 book 节点。 |
attribute::lang | 选取当前节点的 lang 属性。 |
child::* | 选取当前节点的所有子元素。 |
attribute::* | 选取当前节点的所有属性。 |
child::text() | 选取当前节点的所有文本子节点。 |
child::node() | 选取当前节点的所有子节点。 |
descendant::book | 选取当前节点的所有 book 后代。 |
ancestor::book | 选择当前节点的所有 book 先辈。 |
ancestor-or-self::book | 选取当前节点的所有 book 先辈以及当前节点(如果此节点是 book 节点) |
child::*/child::price | 选取当前节点的所有 price 孙节点。 |
四、一些函数
1. starts-with函数
获取以xxx开头的元素
例子:xpath(‘//div[stars-with(@class,”test”)]’)
2 contains函数
获取包含xxx的元素
例子:xpath(‘//div[contains(@id,”test”)]’)
3 and
与的关系
例子:xpath(‘//div[contains(@id,”test”) and contains(@id,”title”)]’)
4 text()函数
例子1:xpath(‘//div[contains(text(),”test”)]’)
例子2:xpath(‘//div[@id=”“test]/text()’)
五、一个lxml的xpath示例
下面这个代码来自http://www.cnblogs.com/descusr/archive/2012/06/20/2557075.html
#coding=utf-8
from lxml import etree
html = '''
<html>
<head>
<meta name="content-type" content="text/html; charset=utf-8" />
<title>友情链接查询 - 站长工具</title>
<!-- uRj0Ak8VLEPhjWhg3m9z4EjXJwc -->
<meta name="Keywords" content="友情链接查询" />
<meta name="Description" content="友情链接查询" />
</head>
<body>
<h1 class="heading">Top News</h1>
<p style="font-size: 200%">World News only on this page</p>
Ah, and here's some more text, by the way.
<p>... and this is a parsed fragment ...</p>
<a href="http://www.cydf.org.cn/" rel="nofollow" target="_blank">青少年发展基金会</a>
<a href="http://www.4399.com/flash/32979.htm" target="_blank">洛克王国</a>
<a href="http://www.4399.com/flash/35538.htm" target="_blank">奥拉星</a>
<a href="http://game.3533.com/game/" target="_blank">手机游戏</a>
<a href="http://game.3533.com/tupian/" target="_blank">手机壁纸</a>
<a href="http://www.4399.com/" target="_blank">4399小游戏</a>
<a href="http://www.91wan.com/" target="_blank">91wan游戏</a>
</body>
</html>
'''
page = etree.HTML(html.lower().decode('utf-8'))
hrefs = page.xpath(u"//a")
for href in hrefs:
print href.attrib
#coding=utf-8
from lxml import etree
html = '''
<html>
<head>
<meta name="content-type" content="text/html; charset=utf-8" />
<title>友情链接查询 - 站长工具</title>
<!-- uRj0Ak8VLEPhjWhg3m9z4EjXJwc -->
<meta name="Keywords" content="友情链接查询" />
<meta name="Description" content="友情链接查询" /></head>
<body>
<h1 class="heading">Top News</h1>
<p style="font-size: 200%">World News only on this page</p>
Ah, and here's some more text, by the way.
<p>... and this is a parsed fragment ...</p><a href="http://www.cydf.org.cn/" rel="nofollow" target="_blank">青少年发展基金会</a>
<a href="http://www.4399.com/flash/32979.htm" target="_blank">洛克王国</a>
<a href="http://www.4399.com/flash/35538.htm" target="_blank">奥拉星</a>
<a href="http://game.3533.com/game/" target="_blank">手机游戏</a>
<a href="http://game.3533.com/tupian/" target="_blank">手机壁纸</a>
<a href="http://www.4399.com/" target="_blank">4399小游戏</a>
<a href="http://www.91wan.com/" target="_blank">91wan游戏</a></body>
</html>'''
page = etree.HTML(html.lower().decode('utf-8'))
hrefs = page.xpath(u"//a")
for href in hrefs:
print href.attrib
打印出的结果为:
{‘href’: ‘http://www.cydf.org.cn/‘, ‘target’: ‘_blank’, ‘rel’: ‘nofollow’}
{‘href’: ‘http://www.4399.com/flash/32979.htm‘, ‘target’: ‘_blank’}
{‘href’: ‘http://www.4399.com/flash/35538.htm‘, ‘target’: ‘_blank’}
{‘href’: ‘http://game.3533.com/game/‘, ‘target’: ‘_blank’}
{‘href’: ‘http://game.3533.com/tupian/‘, ‘target’: ‘_blank’}
{‘href’: ‘http://www.4399.com/‘, ‘target’: ‘_blank’}
{‘href’: ‘http://www.91wan.com/‘, ‘target’: ‘_blank’}
如果要获取标签a之间的内容,就可以用print href.text
输出
六、总结
上面的内容大多都是抄自网上的一些资料。这里只是做了一个大概的总结,后面如果有漏的还会补充。
xPath 用法总结整理的更多相关文章
- scrapy xpath用法
一.实验环境 1.Windows7x64_SP1 2.anaconda3 + python3.7.3(anaconda集成,不需单独安装) 3.scrapy1.6.0 二.用法举例 1.开启scrap ...
- 网络爬虫之Xpath用法汇总
众所周知,在设计爬虫时,最麻烦的一步就是对网页元素进行分析,目前流行的网页元素获取的工具有BeautifulSoup,lxml等,而据我使用的体验而言,Scrapy的元素选择器Xpath(结合正则表达 ...
- Xpath用法
在进行网页抓取的时候,分析定位html节点是获取抓取信息的关键,目前我用的是lxml模块(用来分析XML文档结构的,当然也能分析html结构), 利用其lxml.html的xpath对html进行分析 ...
- Xpath语法格式整理
http://www.cnblogs.com/Loofah/archive/2012/05/10/2494036.html 经常在工作中会使用到XPath的相关知识,但每次总会在一些关键的地方不记得或 ...
- 扩展视图之xpath用法
在视图扩展中,需要定位扩展字段需要显示的位置,通过xpath来实现定位 odoo 视图函数 在整个项目文件中,结构并不是十分明显,虽然它也遵循MVC设计,类比django的MTV模式,各个模块区分的十 ...
- Python re模块, xpath 用法
1.re正则的用法总结 (1). ^ 表示以哪个字符为开头 eg: '^g' 表示以g开头的字符串 . 表示任意字符 '^g.d' 表示以g开头第二个为任意字符,第三个为b的字 ...
- (转)Xpath语法格式整理
原文 经常在工作中会使用到XPath的相关知识,但每次总会在一些关键的地方不记得或不太清楚,所以免不了每次总要查一些零碎的知识,感觉即很烦又浪费时间,所以对XPath归纳及总结一下. 在这篇文章中你将 ...
- GDB用法简要整理
[时间:2017-05] [状态:Open] [关键词:gdb,调试,debug,用户手册] 使用gdb是需要在编译是指定-g命令,在可执行文件中添加符号信息. 1. 启动和退出 可以使用gdb gd ...
- XPath用法详解
1.XPath是什么 XPath 是一门在 XML 文档中查找信息的语言.XPath 用于在 XML 文档中通过元素和属性进行导航(你可以理解为一种类似正则表达式的方法) 2.XPath的语法 表达式 ...
随机推荐
- 一个狗血的mysql编码错误
执行查询语句总是报错,某个查询语句字段编码错误. 各种修改那个表没用, 最后发现是创建schemas的时候没有加编码 应该由 CREATE SCHEMA new_schema;改为 CREATE SC ...
- 查看linux服务器上Tensorflow的版本和位置
查看tensorflow版本,可以在终端输入查询命令如下: python import tensorflow as tf tf.__version__ 查询tensorflow安装路径为: tf.__ ...
- 微信小程序页面跳转导航wx.navigateTo和wx.redirectTo
}) wx.redirectTo(OBJECT) 关闭当前页面,跳转到应用内的某个页面. 还是用上面的三张图示作为例子,当使用wx.redirctTo接口跳转页面时,原来的页面将被删除掉,当然,这是小 ...
- Angular2 之父子组件交互方式
父子组件交互方式,这里介绍主要的三种方式 1.事件传值 下面以列表页和分页组件举例. list.component.html <pagination *ngIf="pageParams ...
- Tensorflow(添加噪声的方式)
在去噪自编码器中,模型的输入是原始的输入经过某种形式的加噪过程后的衰弱的形式,所以加噪声一般分为:加高斯白噪声,掩模噪声,椒盐噪声. 1.加性高斯噪声 self.scale = tf,placehol ...
- mybatis-spring和spring版本搭配问题
所报错误:org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer 匹配的版本(my ...
- 团队服务器搭建(搭建php环境和安装在线mysql管理工具phpmyadmin)
1.本人良心推荐阿里云,因为他对学生来说优惠很多,比如说9.9/月的云主机,所以这里演示阿里云ubuntu系统,系统可以自己安装的 2.来到阿里云官网https://www.aliyun.com,免费 ...
- js 取一定范围内的整数
function selectNum(lowNum,upNum) { var num = upNum-lowNum+1; // Math.floor() 向下取整 return Math.floor( ...
- 使用node.js & live server在移动端测试网站
1.安装node.js 下载Node.js官网 cmd下确认node已安装 node -v ※进入node环境 2.确认npm已安装(Node.js的包管理工具(package manager)) n ...
- SpingMVC的工作流程
一.SpringMVC的主要组件 前端控制器(DisatcherServlet):接收请求,响应结果,返回可以是json,String等数据类型,也可以是页面(Model). 处理器映射器(Handl ...