XPATH CSS PATH CHEAT SHEET






# child::nodename 选取所有属于当前节点的 book 子元素,等价于 './nodename'>>> root.xpath('child::book')
[<Element book at 0x2d888c8>, <Element book at 0x2d88878>]>>> root.xpath('./book')
[<Element book at 0x2d888c8>, <Element book at 0x2d88878>]# attribute::lang 选取当前节点的 lang 属性,等价于 './@lang'>>> root.xpath('//*[@lang]')[0].xpath('attribute::lang')
['eng']>>> root.xpath('//*[@lang]')[0].xpath('@lang')
['eng']# child::* 选取当前节点的所有子元素,等价于 './*'>>> root.xpath('child::*')
[<Element book at 0x2d88878>, <Element book at 0x2d88738>]>>> root.xpath('./*')
[<Element book at 0x2d88878>, <Element book at 0x2d88738>]# attribute::* 选取当前节点的所有属性,等价于 './@*'>>> root.xpath('//*[@*]')[0].xpath('attribute::*')
['eng']>>> root.xpath('//*[@*]')[0].xpath('@*')
['eng']# child::text() 选取当前节点的所有文本子节点,等价于 './text()'>>> root.xpath('child::text()')
['\n    ', '\n    ', '\n']>>> root.xpath('./text()')
['\n    ', '\n    ', '\n']# child::node() 选取当前节点所有子节点,等价于 './node()'>>> root.xpath('child::node()')
['\n    ', <Element book at 0x2d88878>, '\n    ', <Element book at 0x2d88738>, '\n']>>> root.xpath('./node()')
['\n    ', <Element book at 0x2d88878>, '\n    ', <Element book at 0x2d88738>, '\n']# descendant::book 选取当前节点所有 book 后代,等价于 './/book'>>> root.xpath('descendant::book')
[<Element book at 0x2d88878>, <Element book at 0x2d88738>]>>> root.xpath('.//book')
[<Element book at 0x2d88878>, <Element book at 0x2d88738>]# ancestor::book 选取当前节点所有 book 先辈>>> root.xpath('.//title')[0].xpath('ancestor::book')
[<Element book at 0x2d88878>]# ancestor-or-self::book 选取当前节点的所有 book 先辈以及如果当前节点是 book 的话也要选取>>> root.xpath('.//title')[0].xpath('ancestor-or-self::book') 
[<Element book at 0x2d88878>]>>> root.xpath('.//book')[0].xpath('ancestor-or-self::book')
[<Element book at 0x2d88878>]>>> root.xpath('.//book')[0].xpath('ancestor::book')
[]# child::*/child::price 选取当前节点的所有 price 孙节点,等价于 './*/price'>>> root.xpath('child::*/child::price')
[<Element price at 0x2d88878>, <Element price at 0x2d88738>]>>> root.xpath('./*/price')
[<Element price at 0x2d88878>, <Element price at 0x2d88738>]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值