zoukankan      html  css  js  c++  java
  • [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
  • 相关阅读:
    jquery click()方法模拟点击事件对a标签不生效
    键盘keyCode
    java配置好jdk-bash: /usr/bin/java: No such file or directory
    iptables配置顺序-两条规则会忽略后边的
    一些非常实用的JSON 教程
    C#实现json的序列化和反序列化
    [asp.net]C#实现json的序列化和反序列化
    一些JSON 教程
    js+JQuery实现返回顶部功能
    HTML标签总结
  • 原文地址:https://www.cnblogs.com/ifantastic/p/3863827.html
Copyright © 2011-2022 走看看