【大爆炸】xpath axis 语法

http://www.caneman.cn/?p=540

示例HTML <html> <a> contentA <b> conetntB <c> contentB </c> <d type='type1'> contenteD </d> </b> <e> contentE </e> </a> </html> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <html> <a> contentA <b> conetntB <c> contentB </c> <d type='type1'> contenteD </d> </b> <e> contentE </e> </a> </html> case1.获取a标签下所有子标签(不包括a)的内容: xpath("//a/child::*/text()") 1 xpath("//a/child::*/text()") 输出结果:>>['contentB', 'contentE'] case2.获取a标签下所有子孙标签(不包括a)的内容: xpath("//a/descendant::*/text()") 1 xpath("//a/descendant::*/text()") 输出结果:>>['contentB', 'contentC', 'contentD', ' contentcE'] case3.获取a标签下所有子孙标签(包括a)的内容: xpath("//a/descendant-or-self::*/text()") 1 xpath("//a/descendant-or-self::*/text()") 输出结果:>>['contentA', 'contentB', 'contentC', 'contentD', ' contentcE'] case4.获取a标签下所有子孙标签(包括a,但不包括含有属性type的标签)的内容: xpath("//a/descendant-or-self::*[not(@type)]/text()") 1 xpath("//a/descendant-or-self::*[not(@type)]/text()") 输出结果:>>['contentA', 'contentB', 'contentC', 'contentcE'] case5.获取a标签下所有子孙标签(包括a,但不包括c类型的标签)的内容: xpath("//a/descendant-or-self::*[not(name(.)='c')]/text()") 1 xpath("//a/descendant-or-self::*[not(name(.)='c')]/text()") 输出结果:>>['contentA', 'contentB', 'contentD', 'contentcE']

  

示例HTML
<html><a>contentA    <b>     conetntB        <c>           contentB        </c>        <d type='type1'>           contenteD        </d>    </b>    <e>     contentE    </e></a></html>1234567891011121314151617<html><a>contentA    <b>     conetntB        <c>           contentB        </c>        <d type='type1'>           contenteD        </d>    </b>    <e>     contentE    </e></a></html>case1.获取a标签下所有子标签(不包括a)的内容:xpath("//a/child::*/text()")1xpath("//a/child::*/text()")输出结果:>>['contentB',  'contentE']
case2.获取a标签下所有子孙标签(不包括a)的内容:xpath("//a/descendant::*/text()")1xpath("//a/descendant::*/text()")输出结果:>>['contentB',  'contentC',  'contentD', ' contentcE']
case3.获取a标签下所有子孙标签(包括a)的内容:xpath("//a/descendant-or-self::*/text()")1xpath("//a/descendant-or-self::*/text()")输出结果:>>['contentA',  'contentB',  'contentC',  'contentD', ' contentcE']
case4.获取a标签下所有子孙标签(包括a,但不包括含有属性type的标签)的内容:xpath("//a/descendant-or-self::*[not(@type)]/text()")1xpath("//a/descendant-or-self::*[not(@type)]/text()")输出结果:>>['contentA',  'contentB',  'contentC',  'contentcE']
case5.获取a标签下所有子孙标签(包括a,但不包括c类型的标签)的内容:xpath("//a/descendant-or-self::*[not(name(.)='c')]/text()")1xpath("//a/descendant-or-self::*[not(name(.)='c')]/text()")输出结果:>>['contentA',  'contentB',  'contentD',  'contentcE']

posted @ 2021-03-05 16:24  峡谷恶霸  阅读(71)  评论(0编辑  收藏  举报