zoukankan      html  css  js  c++  java
  • xpath

    xpath选择器

    """
    ####
    # 2、xpath选择器
    
    # XPath 是一门在 XML 文档中查找信息的语言
    
    """
    xpath选择
    / 从根节点选取  /a   从根节点开始,往下找a标签(子)
    //从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置  //a 从根节点开始找a标签(子子孙孙中所有a)
    . 	选取当前节点。
    .. 	选取当前节点的父节点。
    @ 	选取属性。
    """
    
    doc='''
    <html>
     <head>
      <base href='http://example.com/' />
      <title>Example website</title>
     </head>
     <body>
      <div id='images'>
       <a href='image1.html' id="xxx">Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
       <h5>test</h5>
       <a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
       <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
       <a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
       <a href='image5.html' class='li li-item' name='items'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
       <a href='image6.html' name='items'><span><h5>test</h5></span>Name: My image 6 <br /><img src='image6_thumb.jpg' /></a>
      </div>
     </body>
    </html>
    '''
    
    
    from lxml import etree
    html=etree.HTML(doc)  # 传字符串
    # html=etree.parse('search.html',etree.HTMLParser())  # 文件
    
    # 1、所有节点
    # a = html.xpath('//*')
    
    # 2 指定节点(结果为列表)
    # a=html.xpath('//head')
    
    # 3 子节点,子孙节点
    # 子节点
    # a=html.xpath('//div/a')
    
    # a=html.xpath('//body/a') #无数据
    # a=html.xpath('//body//a')
    
    # 4 父节点
    # a=html.xpath('//body//a[@href="image1.html"]/..')
    # a=html.xpath('//body//a[@href="image1.html"]')
    # a=html.xpath('//body//a[1]/..')
    # 也可以这样
    # a=html.xpath('//body//a[1]/parent::*')
    
    
    # 5 属性匹配
    # a=html.xpath('//body//a[@href="image1.html"]')
    
    # 6 文本获取   标签后加:/text() ********重点
    # a=html.xpath('//body//a[@href="image1.html"]/text()')
    # a=html.xpath('//body//a/text()')
    
    
    # 7 属性获取  标签后:/@href   ********重点
    # a=html.xpath('//body//a/@href')
    # 注意从1 开始取(不是从0)
    a=html.xpath('//body//a[3]/@href')
    
    
    # 8 属性多值匹配
    #  a 标签有多个class类,直接匹配就不可以了,需要用contains
    # a=html.xpath('//body//a[@class="li"]')
    # a=html.xpath('//body//a[@href="image1.html"]')
    # a=html.xpath('//body//a[contains(@class,"li")]')
    # a=html.xpath('//body//a[contains(@class,"li")]/text()')
    # a=html.xpath('//body//a[contains(@class,"li")]/@name')
    
    
    # 9 多属性匹配 or 和 and (了解)
    # a=html.xpath('//body//a[contains(@class,"li") or @name="items"]')
    # a=html.xpath('//body//a[contains(@class,"li") and @name="items"]/text()')
    # a=html.xpath('//body//a[contains(@class,"li")]/text()')
    
    
    # 10 按序选择
    # a=html.xpath('//a[2]/text()')
    # a=html.xpath('//a[2]/@href')
    # 取最后一个(了解)
    # a=html.xpath('//a[last()]/@href')
    # a=html.xpath('//a[last()]/text()')
    # 位置小于3的
    # a=html.xpath('//a[position()<3]/@href')
    # a=html.xpath('//a[position()<3]/text()')
    # 倒数第一个的前2个
    # a=html.xpath('//a[last()-2]/@href')
    
    
    # 11 节点轴选择
    # ancestor:祖先节点
    # 使用了* 获取所有祖先节点
    # a=html.xpath('//a/ancestor::*')
    
    
    # # 获取祖先节点中的div
    # a=html.xpath('//a/ancestor::div')
    # a=html.xpath('//a/ancestor::div/a[2]/text()')
    # attribute:属性值
    # a=html.xpath('//a[1]/attribute::*')
    # a=html.xpath('//a[1]/@href')
    # child:直接子节点
    # a=html.xpath('//a[1]/child::*')
    # a=html.xpath('//a[1]/img/@src')
    # descendant:所有子孙节点
    # a=html.xpath('//a[6]/descendant::*')
    
    # following:当前节点之后所有节点(递归)
    # a=html.xpath('//a[1]/following::*')
    # a=html.xpath('//a[1]/following::*[1]/@href')
    # following-sibling:当前节点之后同级节点(同级)
    # a=html.xpath('//a[1]/following-sibling::*')
    # a=html.xpath('//a[1]/following-sibling::a')
    # a=html.xpath('//a[1]/following-sibling::*[2]')
    # a=html.xpath('//a[1]/following-sibling::*[2]/@href')
    
    print(a)
    
    """
    
  • 相关阅读:
    Apply SOA Design Patterns with WCF (1) Configuration Centralization (配置集中管理)
    NBearLite PetShop 4.0示例源码
    发布NBearLite中文版完全参考手册 + NBearLite 10分钟入门教程 + NBearLite v1.0.0.7 beta
    NBear WebTest 分享一个基于Web的UnitTest工具
    Linux的rsh配置rhost
    [SCM]源码管理 perforce label实例
    python面试题
    LinuxTips目录下的所有的子目录
    perlcgi基础
    perlcgi命令行调试
  • 原文地址:https://www.cnblogs.com/yafeng666/p/12669382.html
Copyright © 2011-2022 走看看