Xpath运算符

5、position定位

    >>print tree.xpath('//*[@id="testid"]/ol/li[position()=2]/text()')[0]
    >>2

6、条件

    >>定位所有h2标签中text值为`这里是个小标题`
    >>print tree.xpath(u'//h2[text()="这里是个小标题"]/text()')[0]
    >>这里是个小标题

7、函数

    count:统计

    >>print tree.xpath('count(//li[@data])') #节点统计
    >>3.0

    concat:字符串连接

    >>print tree.xpath('concat(//li[@data="one"]/text(),//li[@data="three"]/text())')
    >>13

    string:解析当前节点下的字符

    >>#string只能解析匹配到的第一个节点下的值,也就是作用于list时只匹配第一个
    >>print tree.xpath('string(//li)')
    >>时间

    local-name:解析节点名称

    >>print tree.xpath('local-name(//*[@id="testid"])') #local-name解析节点名称
    >>div

    contains(string1,string2):如果 string1 包含 string2,则返回 true,否则返回 false

    >>tree.xpath('//h3[contains(text(),"H3")]/a/text()')[0] #使用字符内容来辅助定位
    >>百度一下
     
    >>一记组合拳
    >>#匹配带有href属性的a标签的先辈节点中的div,其兄弟节点中前一个div节点下ul下li中text属性包含“务”字的节点的值
    >>print tree.xpath(u'//a[@href]/ancestor::div/preceding::div/ul/li[contains(text(),"务")]/text()')[0]
    >>任务

    not:布尔值(否)

    >>print tree.xpath('count(//li[not(@data)])') #不包含data属性的li标签统计
    >>18.0

    string-length:返回指定字符串的长度

    >>#string-length函数+local-name函数定位节点名长度小于2的元素
    >>print tree.xpath('//*[string-length(local-name())<2]/text()')[0]
    >>百度一下

    组合拳2

    >>#contains函数+local-name函数定位节点名包含di的元素
    >>print tree.xpath('//div[@id="testid"]/following::div[contains(local-name(),"di")]')
    >>[<Element div at 0x225e108>, <Element div at 0x225e0c8>]

    or:多条件匹配

    >>print tree.xpath('//li[@data="one" or @code="84"]/text()') #or匹配多个条件
    >>['1', '84']
    >>#也可使用|
    >>print tree.xpath('//li[@data="one"]/text() | //li[@code="84"]/text()') #|匹配多个条件
    >>['1', '84']

    组合拳3:floor + div除法 + ceiling

    >>#position定位+last+div除法,选取中间两个
    >>tree.xpath('//div[@id="go"]/ul/li[position()=floor(last() div 2+0.5) or position()=ceiling(last() div 2+0.5)]/text()')
    >>['5', '6']

    组合拳4隔行定位:position+mod取余

    >>#position+取余运算隔行定位
    >>tree.xpath('//div[@id="go"]/ul/li[position()=((position() mod 2)=0)]/text()') 

    starts-with:以。。开始

    >>#starts-with定位属性值以8开头的li元素
    >>print tree.xpath('//li[starts-with(@code,"8")]/text()')[0]
    >>84

8、数值比较

    <:小于

    >>#所有li的code属性小于200的节点
    >>print tree.xpath('//li[@code<200]/text()')
    >>['84', '104']

    div:对某两个节点的属性值做除法

    >>print tree.xpath('//div[@id="testid"]/ul/li[3]/@code div //div[@id="testid"]/ul/li[1]/@code')
    >>2.65476190476

    组合拳4:根据节点下的某一节点数量定位

    >>#选取所有ul下li节点数大于5的ul节点
    >>print tree.xpath('//ul[count(li)>5]/li/text()')
    >>['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

9、将对象还原为字符串

    >>> s = tree.xpath('//*[@id="testid"]')[0] #使用xpath定位一个节点
    >>> s
    <Element div at 0x2b6ffc8>
    >>> s2 = etree.tostring(s) #还原这个对象为html字符串
    >>> s2
    '<div id="testid">\n\t\t<h2>&#213;&#226;&#192;&#239;&#202;&#199;&#184;&#246;&#208;&#161;&#177;&#234;&#204;&#226;</h2>\n\t\t<ol>\n\t\t\t<li data="one">1</li>\n\t\t\t<li data="two">2</li>\n\t\t\t<li data="three">3</li>\n\t\t</ol>\n\t\t<ul>\n\t\t\t<li code="84">84</li>\n\t\t\t<li code="104">104</li>\n\t\t\t<li code="223">223</li>\n\t\t</ul>\n\t</div>\n\t'

10、选取一个属性中的多个值

    举例:<div class="mp-city-list-container mp-privince-city" mp-role="provinceCityList">
    选择这个div的方案网上有说用and的,但是似乎只能针对不同的属性的单个值
    本次使用contains
    >>.xpath('div[contains(@class,"mp-city-list-container mp-privince-city")]')
    >>当然也可以直接选取其属性的第二个值
    >>.xpath('div[contains(@class,"mp-privince-city")]')
    >>重点是class需要添加一个@符号
    本次验证否定了网上的and,使用了contains,验证环境在scrapy的response.xpath下

 


作者:whaike
链接:https://www.jianshu.com/p/1575db75670f
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

posted @ 2019-07-08 23:08  蘑菇西餐  阅读(598)  评论(0编辑  收藏  举报