• xpath


    XPath (XML Path Language) 是一门在 XML 文档中查找信息的语言,可用来在 XML 文档中对元素和属性进行遍历。 
    W3School官方文档

    开发工具

    • 开源的XPath表达式编辑工具:XMLQuire(XML格式文件可用)
    • Chrome插件 XPath Helper
    • Firefox插件 XPath Checker
     

    XPath 节点

    节点(Node)

      在 XPath 中,有七种类型的节点:元素、属性、文本、命名空间、处理指令、注释以及文档(根)节点。XML 文档是被作为节点树来对待的。树的根被称为文档节点或者根节点。

    请看下面这个 XML 文档:

    <bookstore>
    
    <book>
      <title lang="en">Harry Potter</title>
      <author>J K. Rowling</author> 
      <year>2005</year>
      <price>29.99</price>
    </book>
    
    </bookstore>

    上面的XML文档中的节点例子:

    <bookstore> (文档节点)
    <author>J K. Rowling</author> (元素节点)
    lang="en" (属性节点) 

    基本值(或称原子值,Atomic value)

    基本值是无父或无子的节点。

    基本值的例子:

    J K. Rowling
    "en"

    项目(Item)

    项目是基本值或者节点。

     

    节点关系

    父(Parent)

    每个元素以及属性都有一个父。

    在下面的例子中,book 元素是 title、author、year 以及 price 元素的父:

    <book>
      <title>Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
    </book>

    子(Children)

    元素节点可有零个、一个或多个子。

    在下面的例子中,title、author、year 以及 price 元素都是 book 元素的子:

    <book>
      <title>Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
    </book>

     同胞(Sibling)

    拥有相同的父的节点

    在下面的例子中,title、author、year 以及 price 元素都是同胞:

    <book>
      <title>Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
    </book>

    先辈(Ancestor)

    某节点的父、父的父,等等。

    在下面的例子中,title 元素的先辈是 book 元素和 bookstore 元素:

    <bookstore>
    
    <book>
      <title>Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
    </book>
    
    </bookstore>

    后代(Descendant)

    某个节点的子,子的子,等等。

    在下面的例子中,bookstore 的后代是 book、title、author、year 以及 price 元素:

    <bookstore>
    
    <book>
      <title>Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
    </book>
    
    </bookstore>
     

     

    XPath 语法

    XPath 使用路径表达式来选取 XML 文档中的节点或节点集。节点是通过沿着路径 (path) 或者步 (steps) 来选取的。

    XML 实例文档

    我们将在下面的例子中使用这个 XML 文档。

    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <bookstore>
    
    <book>
      <title lang="eng">Harry Potter</title>
      <price>29.99</price>
    </book>
    
    <book>
      <title lang="eng">Learning XML</title>
      <price>39.95</price>
    </book>
    
    </bookstore>

    选取节点

    XPath 使用路径表达式在 XML 文档中选取节点。节点是通过沿着路径或者 step 来选取的。

    下面列出了最有用的路径表达式:

    表达式描述
    nodename 选取此节点的所有子节点。
    / 从根节点选取。
    // 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
    . 选取当前节点。
    .. 选取当前节点的父节点。
    @ 选取属性。

    实例

    在下面的表格中,我们已列出了一些路径表达式以及表达式的结果:

    路径表达式结果
    bookstore 选取 bookstore 元素的所有子节点。
    /bookstore

    选取根元素 bookstore。

    注释:假如路径起始于正斜杠( / ),则此路径始终代表到某元素的绝对路径!

    bookstore/book 选取属于 bookstore 的子元素的所有 book 元素。
    //book 选取所有 book 子元素,而不管它们在文档中的位置。
    bookstore//book 选择属于 bookstore 元素的后代的所有 book 元素,而不管它们位于 bookstore 之下的什么位置。
    //@lang 选取名为 lang 的所有属性。

    谓语(Predicates)

    谓语用来查找某个特定的节点或者包含某个指定的值的节点。

    谓语被嵌在方括号中。

    实例

    在下面的表格中,我们列出了带有谓语的一些路径表达式,以及表达式的结果:

    路径表达式结果
    /bookstore/book[1] 选取属于 bookstore 子元素的第一个 book 元素。
    /bookstore/book[last()] 选取属于 bookstore 子元素的最后一个 book 元素。
    /bookstore/book[last()-1] 选取属于 bookstore 子元素的倒数第二个 book 元素。
    /bookstore/book[position()<3] 选取最前面的两个属于 bookstore 元素的子元素的 book 元素。
    //title[@lang] 选取所有拥有名为 lang 的属性的 title 元素。
    //title[@lang='eng'] 选取所有 title 元素,且这些元素拥有值为 eng 的 lang 属性。
    /bookstore/book[price>35.00] 选取 bookstore 元素的所有 book 元素,且其中的 price 元素的值须大于 35.00。
    /bookstore/book[price>35.00]/title 选取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值须大于 35.00。

    选取未知节点

    XPath 通配符可用来选取未知的 XML 元素。

    通配符描述
    * 匹配任何元素节点。
    @* 匹配任何属性节点。
    node() 匹配任何类型的节点。

    实例

    在下面的表格中,我们列出了一些路径表达式,以及这些表达式的结果:

    路径表达式结果
    /bookstore/* 选取 bookstore 元素的所有子元素。
    //* 选取文档中的所有元素。
    //title[@*] 选取所有带有属性的 title 元素。

    选取若干路径

    通过在路径表达式中使用“|”运算符,您可以选取若干个路径。

    实例

    在下面的表格中,我们列出了一些路径表达式,以及这些表达式的结果:

    路径表达式结果
    //book/title | //book/price 选取 book 元素的所有 title 和 price 元素。
    //title | //price 选取文档中的所有 title 和 price 元素。
    /bookstore/book/title | //price 选取属于 bookstore 元素的 book 元素的所有 title 元素,以及文档中所有的 price 元素。

     

     

    Python操作Xpath

    lxml库

    lxml是一个HTML/XML的解析器,主要的功能是如何解析和提取HTML/XML数据 
    我们可以利用xpath语法来快速的定位特定元素以及节点信息

    安装lxml

    pip install lxml

    Python操作xpath简单实例:

    from lxml import etree
    
    html = '''
    <bookstore> 
      <book price="100" category="cooking"> 
        <title lang="en">Everyday Italian</title>  
        <author>Giada De Laurentiis</author>  
        <year>2005</year>  
        <price>30.00</price> 
      </book>  
    
      <book category="children"> 
        <title lang="en">Harry Potter</title>  
        <author>J K. Rowling</author>  
        <year>2005</year>  
        <price>29.99</price> 
      </book>  
    
      <book category="web"> 
        <title category="web">XQuery Kick Start</title>  
        <author>James McGovern</author>  
        <author>Per Bothner</author>  
        <author>Kurt Cagle</author>  
        <author>James Linn</author>  
        <author>Vaidyanathan Nagarajan</author>  
        <year>2003</year>  
        <price>49.99</price> 
      </book> 
    
      <book category="web" cover="paperback"> 
        <title>Learning XML</title>  
        <author>Erik T. Ray</author>  
        <year>2003</year>  
        <price>39.95</price> 
      </book> 
    
    </bookstore>
    
    '''
    html = etree.HTML(html) # 加载字符串
    # html = etree.parse('temp.html') # 加载文件
    
    #构建xpath规则提取数据
    res1 = html.xpath('//bookstore/book/title/text()')
    res2 = html.xpath('//book/@cover | //book/@category')
    res3 = html.xpath('//bookstore/book[1]/price/text()')
    res4 = html.xpath('//bookstore/book[position()<2]') # 获取第一本书  postion就是索引,索引从1开始
    res5 = html.xpath('//title[@lang]') #
    res6 = html.xpath('//title[@lang="en"]/text()') #
    res7 = html.xpath('//bookstore/book[price>35.00]/title/text()') #
    res8 = html.xpath('//bookstore/*') #
    res9 = html.xpath('//bookstore//*') #
    res10 = html.xpath('//title[@*]') #
    res11 = html.xpath('//book/title | //book/price') #
    res12 = html.xpath('//*[@category="web"]')
    
    print(res1)
    print(res2)
    print(res3)
    print(res4)
    print(res5)
    print(res6)
    print(res7)
    print(res8)
    print(res9)
    print(res10)
    print(res11)
    print(res12)

    结果为:

    ['Everyday Italian', 'Harry Potter', 'XQuery Kick Start', 'Learning XML']
    ['cooking', 'children', 'web', 'web', 'paperback']
    ['30.00']
    [<Element book at 0x2cac708>]
    [<Element title at 0x2cac588>, <Element title at 0x2cac6c8>]
    ['Everyday Italian', 'Harry Potter']
    ['XQuery Kick Start', 'Learning XML']
    [<Element book at 0x2cac708>, <Element book at 0x2cac808>, <Element book at 0x2cac848>, <Element book at 0x2cac8c8>]
    [<Element book at 0x2cac708>, <Element title at 0x2cac588>, <Element author at 0x2cacb48>, <Element year at 0x2cacb88>, <Element price at 0x2cac988>, <Element book at 0x2cac808>, <Element title at 0x2cac6c8>, <Element author at 0x2cacc08>, <Element year at 0x2cacc48>, <Element price at 0x2cacbc8>, <Element book at 0x2cac848>, <Element title at 0x2cac688>, <Element author at 0x2cacc88>, <Element author at 0x2caccc8>, <Element author at 0x2cacd08>, <Element author at 0x2cacd48>, <Element author at 0x2cacd88>, <Element year at 0x2cacdc8>, <Element price at 0x2cace08>, <Element book at 0x2cac8c8>, <Element title at 0x2cac748>, <Element author at 0x2cace48>, <Element year at 0x2cace88>, <Element price at 0x2cacec8>]
    [<Element title at 0x2cac588>, <Element title at 0x2cac6c8>, <Element title at 0x2cac688>]
    [<Element title at 0x2cac588>, <Element price at 0x2cac988>, <Element title at 0x2cac6c8>, <Element price at 0x2cacbc8>, <Element title at 0x2cac688>, <Element price at 0x2cace08>, <Element title at 0x2cac748>, <Element price at 0x2cacec8>]
    [<Element book at 0x2cac848>, <Element title at 0x2cac688>, <Element book at 0x2cac8c8>]
  • 相关阅读:
    中国移动 使用Linux、OpenStack
    【 【henuacm2016级暑期训练】动态规划专题 K】 Really Big Numbers
    【【henuacm2016级暑期训练】动态规划专题 J】Red-Green Towers
    【【henuacm2016级暑期训练】动态规划专题 I】Gargari and Permutations
    【【henuacm2016级暑期训练】动态规划专题 H】Greenhouse Effect
    【 【henuacm2016级暑期训练】动态规划专题 G】 Palindrome pairs
    【【henuacm2016级暑期训练】动态规划专题 F】Physics Practical
    【【henuacm2016级暑期训练】动态规划专题 E】Destroying Roads
    【【henuacm2016级暑期训练】动态规划专题 D】Writing Code
    【henuacm2016级暑期训练-动态规划专题 C】Little Girl and Maximum XOR
  • 原文地址:https://www.cnblogs.com/mswei/p/9337734.html
Copyright © 2020-2023  润新知