zoukankan      html  css  js  c++  java
  • 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()")) #取出内部<>数据
  • 相关阅读:
    oracle 同义词
    Git分支操作
    [转]Hibernate状态 (merge,attachDirty,attachClean)
    [转]对jQuery的事件绑定的一些思考
    [转]为什么用Spring来管理Hibernate?
    [转]spring注入静态bean
    ajax 请求调用问题
    Java常用开发组件介绍
    Spring @Value SpEl 知识点小记
    radio中最佳解决方案
  • 原文地址:https://www.cnblogs.com/my-global/p/12454066.html
Copyright © 2011-2022 走看看