lxml中的xpat详细使用介绍
lxml中的xpat详细使用介绍:
import lxml import lxml.etree html=lxml.etree.parse("index.html") print(type(html)) res=html.xpath("//li") #res是一个列表,包含所有元素 print(len(res)) print(type(res)) #list print(type(res[0])) #节点 #//选取所有book 子元素 #/选择路径目录,选择一个 #@ 选择属性 print(html.xpath("//li/@class")) #取出li的所有节点class名称 print(html.xpath("//li/@text")) # print(html.xpath("//li/a")) #li下面5个节点,每个节点对于一个元素 print(html.xpath("//li/a/@href")) #取出li的所有节点 a内部href名称 print(html.xpath("//li/a/@href=\"link3.html\"")) #判断是有一个节点==link3.html print(html.xpath("//li/span")) #取出li下面的所有span print(html.xpath("//li/span/@class")) #取出li下面所有的span内部的class print(html.xpath("//li//a/@class")) #取出li的所有节点内部节点a包含的class print(html.xpath("//li")) #取出所有节点 print(html.xpath("//li[1]")) #取出第一个节点 print(html.xpath("//li[last()]")) #取出最后一个 print(html.xpath("//li[last()-1]")) #取出倒数第二个 #print(html.xpath("//li[last()-1])/a/@href") #取出倒数第二个a下面的href print(html.xpath("//*[@text=\"3\"]"))#选着text=3的元素 print(html.xpath("//*[@text=\"3\"]/@class")) #选着text=3的元素 print(html.xpath("//*[@class=\"nimei\"]")) print(html.xpath("//li//a/text()")) #text() 取出<> print(html.xpath("//li[3]/a/span/text()")) #取出内部<>数据
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?