zoukankan      html  css  js  c++  java
  • xpath轴

    child::book    选取所有属于当前节点的子元素的 book 节点。
    attribute::lang    选取当前节点的 lang 属性。
    child::*    选取当前节点的所有子元素。
    attribute::*    选取当前节点的所有属性。
    child::text()    选取当前节点的所有文本子节点。
    child::node()    选取当前节点的所有子节点。
    descendant::book    选取当前节点的所有 book 后代。
    ancestor::book    选择当前节点的所有 book 先辈。
    ancestor-or-self::book    选取当前节点的所有 book 先辈以及当前节点(如果此节点是 book 节点)
    child::*/child::price    选取当前节点的所有 price 孙节点。
    from lxml import etree
    
    html = '''
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <!--网页头部信息-->
        <title>网页名</title>
    </head>
    <body>
        <!--下面是网页正文-->
        <div class="two" name='test' style="color:red">id-text</div>
        <div class="one two">class-text</div>
        <div class="one">class-span</div>
        <div class="three">three</div>
    </body>
    </html>
    '''
    
    html =etree.HTML(html)
    
    content1 = html.xpath("//div/ancestor::*") #选取div的所有父和祖父节点
    content1 = html.xpath("//div[@class='two']/attribute::*") #选取节点的所有属性
    content1 = html.xpath("//div[@class='two']/attribute::style") #选取节点的style属性
    content1 = html.xpath(".//body/child::div") #选取所有子节点
    
    print(content1)
    

      

  • 相关阅读:
    生活的乐趣
    android加载webview白屏问题
    强极则辱
    向studio项目中复制集成其他代码,项目R文件丢失
    每一步都是最好的选择
    JQuery小插件,Selected插件1
    JSON进阶三JSON的几种调用形式
    JSON进阶四前后台交互之美
    .NET双样式分页控件
    JSON进阶五JS和WCF的交互
  • 原文地址:https://www.cnblogs.com/brady-wang/p/12462026.html
Copyright © 2011-2022 走看看