xpath的contains使用方法

本文介绍了XPath选择器中contains函数的使用,通过示例详细解释了如何利用contains匹配包含特定文本的元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xpath的contains使用方法

示例:

#它会取class含有有test1和test2的元素
xpath('//div[contains(@class,"test1") and contains(@class,"test2")]') 
#它会取class 含有 test1 或者test2满足时,或者同时满足时的元素
xpath('//div[contains(@class,"test1") or contains(@class,"test2")]') 
查找name属性中包含zhangsan关键字的页面元素
//divt[contains(@name,'zhangsan')] 
xpath写法为 //a[text()='百度搜索'] 或者 //a[contains(text(),"百度搜索")]
<a href="http://www.baidu.com">百度搜索</a>
#开闭标签之间的文本内容
//a[contains(@class,"news-item-title")]/text()  
#选中标签节点中获取指定属性的值
//a[contains(@class,"news-item-title")]/@href  
# 选取class属性包含ing的href链接地址
html.xpath("//div/p[2][contains(@class,'ing')]/a/@href")
# 选取价格大于20元书的价格值
html.xpath("//book[price>20.00]/price/text()")
# 选取前2本书
html.xpath("//book[position()<3]/title/text()")
#取div位置大于2的 并且类包含three的
html.xpath("//div[position()>2 and contains(@class,'three')]/@class")
# 豆瓣电影评分大于8的电影
//span[@class="rating_nums"][text()>8]
#xpath定位标签中最后一个元素 last()
//span[@class="comment-info"]//span[last()]
#xpath定位标签中倒数第二个元素 last()
//span[@class="comment-info"]//span[last()-1]
#列表下一页 text()
//div[@class="m-page"]/a[contains(text(),'下一页')]
#在路径表达式中使用"|"运算符,您可以选取若干个路径。
#选取 book 元素的所有 title 和 price 元素。
//book/title | //book/price	
#选取文档中的所有 title 和 price 元素。
//title | //price	
#选取属于 bookstore 元素的 book 元素的所有 title 元素,以及文档中所有的 price 元素。
//bookstore/book/title | //price	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值