Lxml库及Xpath语法详解

一、Xpath术语

1、节点:

七种类型的节点:元素、属性、文本、命名空间、处理指令、注释以及文档(根)节点

2、节点关系:

父、子、同胞、先辈、后代

3、节点选取

表达式 描述
nodename 选取此节点的所有子节点
/ 从根节点选取
// 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
. 选取当前节点
.. 选取当前节点的父节点
@ 选取属性

实例
在下面的表格中,我们已列出了一些路径表达式以及表达式的结果:

路径表达式 结果
bookstore 选取 bookstore 元素的所有子节点
/bookstore 选取根元素 bookstore。注释:假如路径起始于正斜杠( / ),则此路径始终代表到某元素的绝对路径!
bookstore/book 选取属于 bookstore 的子元素的所有 book 元素
//book 选取所有 book 子元素,而不管它们在文档中的位置。
bookstore//book 选择属于 bookstore 元素的后代的所有 book 元素,而不管它们位于 bookstore 之下的什么位置。
//@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元素

4、XPath运算符

|、+、-、*、div、=、!=、<、<=、>、>=、or、and、mod

5、XPath实例

from lxml import etree

text = """
<div class="wrapper">
    <i class="iconfont icon-back" id="back"></i>
    <a href="/" id="channel">新浪社会</a>
    <ul id="nav">
        <li><a href="http://domestic.firefox.sina.com/" title="国内">国内</a></li>
        <li><a href="http://world.firefox.sina.com/" title="国际">国际</a></li>
        <li><a href="http://mil.firefox.sina.com/" title="军事">军事</a></li>
        <li><a href="http://photo.firefox.sina.com/" title="图片">图片</a></li>
        <li><a href="http://society.firefox.sina.com/" title="社会">社会</a></li>
        <li><a href="http://ent.firefox.sina.com/" title="娱乐">娱乐</a></li>
        <li><a href="http://tech.firefox.sina.com/" title="科技">科技</a></li>
        <li><a href="http://sports.firefox.sina.com/" title="体育">体育</a></li>
        <li><a href="http://finance.firefox.sina.com/" title="财经">财经</a></li>
        <li><a href="http://auto.firefox.sina.com/" title="汽车">汽车</a></li>
    </ul>
    <i class="iconfont icon-liebiao" id="menu"></i>
</div>
"""

# 创建html对象
html = etree.HTML(text)

# 获取所有a标签的href内容
results = html.xpath('//a/@href')
print(results)
# 获取所有id为channel的a标签的href内容
results2 = html.xpath('//a[@id="channel"]/@href')
print(results2)
# 获取所有li标签下的a标签的文本内容
results3 = html.xpath('//li/a/text()')
print(results3)

运行结果如下(都是列表):

['/', 'http://domestic.firefox.sina.com/', 'http://world.firefox.sina.com/', 'http://mil.firefox.sina.com/', 'http://photo.firefox.sina.com/', 'http://society.firefox.sina.com/', 'http://ent.firefox.sina.com/', 'http://tech.firefox.sina.com/', 'http://sports.firefox.sina.com/', 'http://finance.firefox.sina.com/', 'http://auto.firefox.sina.com/']
['/']
['新浪社会']
['国内', '国际', '军事', '图片', '社会', '娱乐', '科技', '体育', '财经', '汽车']

参考链接:

https://cuiqingcai.com/2621.html

http://www.cnblogs.com/zhaof/p/7189860.html



猜你喜欢

转载自blog.csdn.net/weixin_41601173/article/details/80021977