杨梅冲
每天在想什么呢?
随笔 - 198,  文章 - 0,  评论 - 8,  阅读 - 17万

Xpath基础使用方法

复制代码
'''谷歌浏览器右键html标签可以直接拷贝css选择器和xpath选择器'''
# s-top-left > a:nth-child(4)
# //*[@id="s-top-left"]/a[4]

# 基本语法
# 模拟爬取过来的html页面数据
doc='''
<html>
 <head>
  <base href='http://example.com/' />
  <title>Example website</title>
 </head>
 <body>
  <div id='images'>
   <a href='image1.html' a="xxx">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' class='li'>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
# from bs4 import BeautifulSoup
html=etree.HTML(doc)  # 类似于bs4
# soup = BeautifulSoup(doc,'lxml')
# html=etree.parse('search.html',etree.HTMLParser())
# 1 所有节点
# a=html.xpath('//*')  # 匹配所有标签
# # 2 指定节点(结果为列表)
# a=html.xpath('//head')  # 标签选择器
# # 3 子节点,子孙节点
# a=html.xpath('//div/a')  # 儿子选择器
# a=html.xpath('//body//a')  # 后代选择器
# # 4 父节点
# a=html.xpath('//body//a[@href="image1.html"]')
# a=html.xpath('//body//a[@href="image1.html"]/..')  # 查看父标签
# a=html.xpath('//body//a[1]')  # 查找body后代里面的第一个a标签(在xpath中起始位置就是从1开始)

# # 5 属性匹配  css里面的属性选择器
# a=html.xpath('//body//a[@href="image1.html"]')
#
# # 6 文本获取
# a=html.xpath('//body//a[@href="image1.html"]/text()')
# a=html.xpath('//body//a/text()')
#
# # 7 属性获取
# a=html.xpath('//body//a/@href')
# # # 注意从1 开始取(不是从0)
# a=html.xpath('//body//a[2]/@href')
# # 8 属性多值匹配
# #  a 标签有多个class类,直接匹配就不可以了,需要用contains
# a=html.xpath('//body//a[@class="li"]')  # 精确查找
# a=html.xpath('//body//a[contains(@class,"li")]/text()')
# # 9 多属性匹配
# a=html.xpath('//body//a[contains(@class,"li") or @name="items"]')
# a=html.xpath('//body//a[contains(@class,"li") and @name="items"]/text()')
# a=html.xpath('//body//a[contains(@class,"li")]/text()')
# # 10 按序选择
# a=html.xpath('//a[2]/text()')
# a=html.xpath('//a[2]/@href')
# # 取最后一个
# a=html.xpath('//a[last()]/@href')
# # 位置小于3的      类似于切片取值
# a=html.xpath('//a[position()<3]/@href')
# # 倒数第二个
# a=html.xpath('//a[last()-2]/@href')
# attribute:属性值
# a=html.xpath('//a[1]/attribute::*')

'''了解'''
# # 11 节点轴选择
# # ancestor:祖先节点
# # 使用了* 获取所有祖先节点
# a=html.xpath('//a/ancestor::*')
# # # 获取祖先节点中的div
# a=html.xpath('//a/ancestor::div')
# # attribute:属性值
# a=html.xpath('//a[1]/attribute::*')
# # child:直接子节点
# a=html.xpath('//a[1]/child::*')
# # descendant:所有子孙节点
# a=html.xpath('//a[6]/descendant::*')
# # following:当前节点之后所有节点
# a=html.xpath('//a[1]/following::*')
# a=html.xpath('//a[1]/following::*[1]/@href')
# # following-sibling:当前节点之后同级节点
# a=html.xpath('//a[1]/following-sibling::*')
# a=html.xpath('//a[1]/following-sibling::a')
# a=html.xpath('//a[1]/following-sibling::*[2]/text()')
# a=html.xpath('//a[1]/following-sibling::*[2]/@href')
print(a)

复制代码

 

posted on   杨梅冲  阅读(116)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示