[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
posted @ 2014-07-27 01:33  iFantasticMe  阅读(1343)  评论(0编辑  收藏  举报