码迷,mamicode.com
首页 > 其他好文 > 详细

xpath选择器的使用

时间:2020-08-05 00:06:37      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:follow   XML   使用   pos   标签   text   语言   方式   pytho   

xpath简介

xpath是一门可以在xml文档中查找信息的语言
/: 从根节点选取
//: 不管位置,直接找
/@href 属性名
/text()

xpath选择器操作

doc=‘‘‘
<html>
 <head>
  <base href=‘http://example.com/‘ />
  <title>Example website</title>
 </head>
 <body>
  <div id=‘images‘>
   <a href=‘image1.html‘ aa=‘bb‘>Name: My image 1 <br /><img src=‘image1_thumb.jpg‘ /></a>
   <a href=‘image2.html‘>Name: My image 2 <br /><img src=‘image2_thumb.jpg‘ /></a>
   <a href=‘image3.html‘>Name: My image 3 <br /><img src=‘image3_thumb.jpg‘ /></a>
   <a href=‘image4.html‘>Name: My image 4 <br /><img src=‘image4_thumb.jpg‘ /></a>
   <a href=‘image5.html‘ class=‘li li-item‘ name=‘items‘>Name: My image 5 <br /><img src=‘image5_thumb.jpg‘ /></a>
   <a href=‘image6.html‘ name=‘items‘><span><h5>test</h5></span>Name: My image 6 <br /><img src=‘image6_thumb.jpg‘ /></a>
  </div>
 </body>
</html>
‘‘‘

使用方式

# 方式一
from lxml import etree
html = etree.parse(‘source.html‘,parser=etree.HTMLParser()) # 打开source.html
# 方式二
from lxml import etree
html = etree.HTML(doc)
  • 查所有的节点
query_list = html.xpath(‘//*‘) 
  • 查询指定的节点
query_list = html.xpath(‘//head‘) # [<Element head at 0x7efd6ee663c0>]
  • 查询子节点,子孙节点
query_list = html.xpath(‘//div/a‘) 
query_list = html.xpath(‘//body/a‘) #[]
query_list = html.xpath(‘//body//a‘)
  • 父节点
query_list = html.xpath(‘//body//a[@href="image2.html"]/..‘) # [<Element div at 0x7f46fde88300>]

query_list = html.xpath(‘//body//a[1]/..‘)
# 也可以这样
query_list = html.xpath(‘//body//a[1]/parent::*‘)
  • 属性匹配
query_list = html.xpath(‘//body//a[@href="image2.html"]‘)
  • 文本获取text() (重要)
query_list = html.xpath(‘//body//a[@href="image2.html"]/text()‘)
# [‘Name: My image 2 ‘]
query_list = html.xpath(‘//body//a/text()‘)
# [‘Name: My image 1 ‘, ‘Name: My image 2 ‘, ‘Name: My image 3 ‘, ‘Name: My image 4 ‘, ‘Name: My image 5 ‘, ‘Name: My image 6 ‘]
  • 属性获取 @href
query_list = html.xpath(‘//body//a/@href‘) # [‘image1.html‘, ‘image2.html‘, ‘image3.html‘, ‘image4.html‘, ‘image5.html‘, ‘image6.html‘]

# 注意列表是从1开始取值,不是0
query_list = html.xpath(‘//body//a[2]/@href‘) #[‘image2.html‘]
  • 属性多值匹配
# 当a标签有多个属性值,直接匹配就不行了,需要使用模糊匹配
query_list = html.xpath(‘//body//a[@class="li"]‘) #[]
query_list = html.xpath(‘//body//a[contains(@class,"li")]‘) #[<Element a at 0x7fcf223f90c0>]

query_list = html.xpath(‘//body//a[contains(@class,"li")]/text()‘) #[‘Name: My image 5 ‘]
  • 多属性匹配
query_list = html.xpath(‘//body//a[contains(@class,"li") or @name="items"]‘)

query_list = html.xpath(‘//body//a[contains(@class,"li") and @name="items"]/text()‘)

query_list = html.xpath(‘//body//a[contains(@class,"li")]/text()‘)
  • 按顺序进行选择
# query_list = html.xpath(‘//a[2]/text()‘)
# query_list = html.xpath(‘//a[2]/@href‘)
# 取最后一个
# query_list = html.xpath(‘//a[last()]/@href‘)
# 位置小于3的
# query_list = html.xpath(‘//a[position()<3]/@href‘)
# 倒数第二个
# query_list = html.xpath(‘//a[last()-2]/@href‘)
  • 节点轴选择
# ancestor:祖先节点
# 使用了* 获取所有祖先节点
# query_list = html.xpath(‘//a/ancestor::*‘)
# # 获取祖先节点中的div
# query_list = html.xpath(‘//a/ancestor::div‘)
# attribute:属性值
# query_list = html.xpath(‘//a[1]/attribute::*‘)
# query_list = html.xpath(‘//a[1]/@aa‘)
# child:直接子节点
# query_list = html.xpath(‘//a[1]/child::*‘)
# query_list = html.xpath(‘//a[1]/child::img/@src‘)
# descendant:所有子孙节点
# query_list = html.xpath(‘//a[6]/descendant::*‘)
# query_list = html.xpath(‘//a[6]/descendant::h5/text()‘)
# following:当前节点之后所有节点(兄弟节点和兄弟内部的节点)
# query_list = html.xpath(‘//a[1]/following::*‘)
# query_list = html.xpath(‘//a[1]/following::*[1]/@href‘)
# following-sibling:当前节点之后同级节点(只找兄弟)
# query_list = html.xpath(‘//a[1]/following-sibling::*‘)
# query_list = html.xpath(‘//a[1]/following-sibling::a‘)
# query_list = html.xpath(‘//a[1]/following-sibling::*[2]‘)
# query_list = html.xpath(‘//a[1]/following-sibling::*[2]/@href‘)

xpath选择器的使用

标签:follow   XML   使用   pos   标签   text   语言   方式   pytho   

原文地址:https://www.cnblogs.com/surpass123/p/13436117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!