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
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术