html,XPath 学习笔记

XPath

1. 

Chrome和傲游浏览器的Console中执行的Xpath代码有些区别。

例如,要提取a tag中的某个属性,在傲游中是这样写:

$x('//a/@href')

而在Chrome中则是

$x('//a')[0].getAttribute('href')


2:// 和 /

在XPath中,双斜线 "//" 表示收集在Hierarchy中任意位置的某类型的元素。比如 “//a” 就表示此文件中的所有 a tag。

而单斜线 "/" 则表示属于某一个tag下一级的tag,比如 "//div/p" 就表示直接位于所有div下一级的p tag。


3:*

用 '*' 来选择某一层下的所有元素.

例:$x('//div/*')


4:select tags with consitions

$x('//a[@href]') # a tags with href attribute

$x('//a[@href="http://www.iana.org/domains/example"]') # a tags whose href is as above

$x('//a[contains(@href, "iana")]') # a tags whose href contains "iana"

$x('//a[starts-with(@href, "http://www.")]') # a tags whose href start with "http://www."

$x('//a[not(contains(@href, "abc"))]') # a tags whose href do not contain "abc"

5 更多XPath命令请见

http://www.w3schools.com/xsl/xsl_functions.asp



6 在网页源码处右键Copy XPath其实是个非常实用的办法。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值