xpath的contains使用方法

发布时间 2023-08-31 08:41:00作者: 祺琪
#它会取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