全称是 XML Path Language,XML路径语言,它是一门在XML文档中和HTML文档中查找信息的语言
1.XPath常用规则
表达式 描述
nodename 选取此节点的所有子节点
/ 从当前节点选取直接子节点
// 从当前节点选取子孙节点
. 选取当前节点
.. 选取当前节点的父节点
@ 选取属性
2.准备工作:安装 lxml 库
3.例子:
from lxml import etree text = ’’’ <div> <ul> <li class="ex1"><a href="ex1.html">ex1</a></li> <li class="ex2"><a href="ex2.html">ex2</a> </ul> </div> ’’’ html = etree.HTML(text) # 调用HTML类进行html初始化工作 r = etree.tostring(html) # 修复HTML代码,补全其他选项 print(r.decode(’utf-8’)) # 结果返回是bytes,我们将其转化成UTF-8 4.所有节点
选取所有节点:
from lxml import etree html = etree.parse(’./test.html’,etree.HTMLParser()) res = html.xpath(’//*’) # 选取所有 print(res)
5.子节点
选取li节点的所有直接a子节点:
from lxml import etree html = etree.parse(’./test.html’,etree.HTMLParser()) res = html.xpath(’//li/a’) print(res)
6.父节点
使用.和..
7.属性匹配
from lxml import etree html = etree.parse(’./test.html’,etree.HTMLParser()) res = html.xpath(’//li[@class=’ex1’]’) print(res) 8.文本属性
选取li节点的内部文本,两种方法,推荐第二种
a.
from lxml import etree html = etree.parse(’./test.html’,etree.HTMLParser()) res = html.xpath(’//li[@class=’ex1’]/a/text()’) print(res) b.推荐,信息更全
from lxml import etree html = etree.parse(’./test.html’,etree.HTMLParser()) res = html.xpath(’//li[@class="ex1"]//text()’) print(res)
9.属性获取
获取所有li节点下所有a节点的href属性
from lxml import etree html = etree.parse(’./test.html’,etree.HTMLParser()) res = html.xpath(’//li/a/@href’) print(res)
10.属性多值匹配
from lxml import etree text = ’’’ <div> <ul> <li class="li li-first"><a href="ex1.html">li1</a></li> </ul> </div> ’’’ html = etree.HTML(text) res = html.xpath(’//li[contains(@class,"li")]/a/text()’) print(res)
【注】
contains()中,
第一个参数传入属性名称,第二个参数传入属性值
11.多属性匹配
根据多个属性确定一个节点
from lxml import etree text = ’’’ <div> <ul> <li class="li" name="123"><a href="ex1.html">ex1</a></li> </ul> </div> ’’’ html = etree.HTML(text) res = html.xpath(’//li[@contains(@class,"li") and @name="123"]/a/text()’) print(res)
12.按序选择(多个节点)
from lxml import etree text = ’’’ <div> <ul> <li class="ex1"><a href="ex1.html">ex1</a></li> <li class="ex2"><a href="ex2.html">ex2</a></li> <li class="ex3"><a href="ex3.html">ex3</a></li> </ul> </div> ’’’ html = etree.HTML(text) res = html.xpath(’//li[1]/a/text()’) # 第一个li res = html.xpath(’//li[last()]/a/text()’) # 最后一个li res = html.xpath(’//li[position()<3]/a/text()’) # 前两个li res = html.xpath(’//li[last()-2]/a/text()’) # 第一个li
【注】
序号从1开始
13.节点轴选择
from lxml import etree text = ’’’ <div> <ul> <li class="ex1"><a href="ex1.html">ex1</a></li> <li class="ex2"><a href="ex2.html">ex2</a></li> <li class="ex3"><a href="ex3.html">ex3</a></li> </ul> </div> ’’’ html = etree.HTML(text) res = html.xpath(’//li[1]/ancestor::*’) # 获取祖先节点 res = html.xpath(’//li[1]/ancestor::div’) # 获取祖先div节点 res = html.xpath(’//li[1]/attribute::*’) # 所有属性值 res = html.xpath(’//li[1]/child::a[href="ex1.html"]’) # 所有直接子节点 res = html.xpath(’//li[1]/descendant::span’) # 所有子孙节点 res = html.xpath(’//li[1]/following::*[2]’) # 当前节点之后的所有节点 res = html.xpath(’//li[1]/following-sibling::*’) # 当前节点之后的所有同级节点