使用xpath定位元素

一、xpath的基本语法


xpath(query) :返回query表达式对应的所有节点的selector list。

>>> response.xpath('//div[@class="quote"]//small/text()')
[<Selector xpath='//div[@class="quote"]//small/text()' data='Albert Einstein'>, <Selector xpath='//div[@class="quote"]//small/text()' data='J.K. Rowling'>, <Selector xpath='//div[@class="quote"]//small/text()' data='Albert Einstein'>, <Selector xpath='//div[@class="quote"]//small/text()' data='Jane Austen'>, <Selector xpath='//div[@class="quote"]//small/text()' data='Marilyn Monroe'>, <Selector xpath='//div[@class="quote"]//small/text()' data='Albert Einstein'>, <Selector xpath='//div[@class="quote"]//small/text()' data='André Gide'>, <Selector xpath='//div[@class="quote"]//small/text()' data='Thomas A. Edison'>, <Selector xpath='//div[@class="quote"]//small/text()' data='Eleanor Roosevelt'>, <Selector xpath='//div[@class="quote"]//small/text()' data='Steve Martin'>]

extract():序列化该节点为Unicode字符串并返回list。

>>> response.xpath('//div[@class="quote"]//small/text()').extract()
['Albert Einstein', 'J.K. Rowling', 'Albert Einstein', 'Jane Austen', 'Marilyn Monroe', 'Albert Einstein', 'André Gide', 'Thomas A. Edison', 'Eleanor Roosevelt', 'Steve Martin']

extract()[0] :取出list中的值。

>>> response.xpath('//div[@class="quote"][1]//small/text()').extract()[0]
'Albert Einstein'

二、xpath通过节点属性获取节点


div[@属性="属性值"]

//th[@class="common"]

xpath获取节点属性值

/@属性

//th/em/following-sibling::a[1]/@href   # 可以得到href属性的值 组成的selector list

三、xpath获取元素节点中的文本


xpath定位到的是节点本身,要想获取到节点中的文本,需要使用/text()

代码如下:

//tbody[@id ="separatorline"]/following-sibling::tbody//th/em/following-sibling::a[1]/text()

四、xpath定位兄弟元素


  • following-sibling   随后的兄弟元素
  • preceding-sibling  之前的兄弟元素

例:定位51testing论坛里的帖子,不含上方广告类。

查看代码,会发现每个帖子由一个tbody组成,但tbody没有任何属性可以使用,唯一一个id还是动态的。尝试着找到了“版块主题”,发现有个可以使用的id,和其余tbody属于兄弟节点。于是就可以使用“版块主题”来定位它下面的所有tbody。xpth代码如下:

//tbody[@id="separatorline"]/following-sibling::tbody

::   表示当前节点的父节点。

想要定位“版块主题”上方的tbody可以使用:

//tbody[@id="separatorline"]/preceding-sibling::tbody

想要定位“版块主题”下方的第一个tbody可以使用:

//tbody[@id="separatorline"]/following-sibling::tbody[1]

想要定位“版块主题”下方的第N个tbody可以使用:

//tbody[@id="separatorline"]/following-sibling::tbody[N]

五、xpath运算符

运算符描述实例返回值
|计算两个节点集//book | //cd返回所有拥有 book 和 cd 元素的节点集
    
=等于price=9.80

如果 price 是 9.80,则返回 true。

如果 price 是 9.90,则返回 false。

!=不等于price!=9.80

如果 price 是 9.90,则返回 true。

如果 price 是 9.80,则返回 false。

<小于price<9.80

如果 price 是 9.00,则返回 true。

如果 price 是 9.90,则返回 false。

<=小于或等于price<=9.80

如果 price 是 9.00,则返回 true。

如果 price 是 9.90,则返回 false。

>大于price>9.80

如果 price 是 9.90,则返回 true。

如果 price 是 9.80,则返回 false。

>=大于或等于price>=9.80

如果 price 是 9.90,则返回 true。

如果 price 是 9.70,则返回 false。

orprice=9.80 or price=9.70

如果 price 是 9.80,则返回 true。

如果 price 是 9.50,则返回 false。

andprice>9.00 and price<9.90

如果 price 是 9.80,则返回 true。

如果 price 是 8.50,则返回 false。

    

 

最近有点小浮躁,静不下心来~~~

想要记录自己学习的每一个过程,便于自己查阅,或许也可以帮助正迷惑的你。

最近推荐一个插件xpath helper,在使用xpath定位元素时十分的方便。

 

  • 14
    点赞
  • 98
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值