XPATH CSS PATH CHEAT SHEET

来源:互联网 发布:淮南大数据公司 编辑:程序博客网 时间:2024/04/30 11:19






# 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>]

原创粉丝点击