• [XPath] XPath 与 lxml (四)XPath 运算符


    XPath 中支持的运算符

    # | 或: 返回所有 price 和 title 节点集合
    >>> root.xpath('//price|//title')
    [<Element title at 0x2d888c8>, <Element price at 0x2d88878>, <Element title at 0x2d88940>, <Element price at 0x2d88738>]
    
    # + 加法:返回所有 price 子元素前两个之和
    >>> root.xpath('//price[1]/text() + //price[2]/text()')
    69.94
    
    # - 减法:返回所有 price 子元素前两个之差
    >>> root.xpath('//price[1]/text() - //price[2]/text()')
    -9.960000000000004
    
    # * 乘法:返回所有 price 子元素前两个之积
    >>> root.xpath('//price[1]/text() * //price[2]/text()')
    1198.1005
    
    # div 除法:返回所有 price 子元素前两个之商
    >>> root.xpath('//price[1]/text() div //price[2]/text()')
    0.7506883604505631
    
    # = 等于:返回 True 或  False
    >>> root.xpath('//price[1]/text() = //price[2]/text()')
    False
    
    # != 不等于:返回 True 或 False
    >>> root.xpath('//price[1]/text() != //price[2]/text()')
    True
    
    # < 小于:返回 True 或 False
    >>> root.xpath('//price[1]/text() < //price[2]/text()')
    True
    
    # <= 小于或等于:返回 True 或 False
    >>> root.xpath('//price[1]/text() <= //price[2]/text()')
    True
    
    # > 大于:返回 True 或 False
    >>> root.xpath('//price[1]/text() > //price[2]/text()')
    False
    
    # >= 大于或等于:返回 True 或 False
    >>> root.xpath('//price[1]/text() >= //price[2]/text()')
    False
    
    # or 或:返回 True 或 False
    >>> root.xpath('//price[1]/text() > 29.9 or //price[2]/text() > 40')
    True
    
    # and 与:返回 True 或 False
    >>> root.xpath('//price[1]/text() > 29.9 and //price[2]/text() > 40')
    False
    
    # mod 求余
    >>> root.xpath('11 mod 2')
    1.0
  • 相关阅读:
    poj2679
    poj2709
    poj1521
    poj2054
    静脉曲张病案
    眩晕耳鸣病案
    声嘶治验
    甘露消毒丹治疗高热不退一例
    黄芩汤加减治疗腹痛一例
    自残症治愈案
  • 原文地址:https://www.cnblogs.com/ifantastic/p/3863827.html
Copyright © 2020-2023  润新知