问题总结
一、高频 xpath
1. 选取节点
表达式 | 描述 | 相关链接 |
---|---|---|
nodename | 选取此节点的所有子节点 | 查看这篇文章 |
/ | 从根节点选取 | 查看这篇文章 |
// | 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置 | 查看这篇文章 |
. | 选取当前节点 | 查看这篇文章 |
… | 选取父节点 | 查看这篇文章 |
@ | 选取属性 | 查看这篇文章 |
text() | 选取文本 |
2. XPath轴
表达式 | 描述 | 相关链接 |
---|---|---|
ancestor ancestor-or-self attribute child descendant descendant-or-self following following-sibling namespace parent preceding preceding-sibling self | 轴可定义相对于当前节点的节点集 | 查看这篇文章 |
3.xpath谓语
表达式 | 描述 | 相关链接 |
---|---|---|
/bookstore/book[1] /bookstore/book[last()] /bookstore/book[last()-1] /bookstore/book[position() < 3] //title[@lang] //title[@lang=‘eng’] /bookstore/book[price>35.00] /bookstore/book[price>35.00] | 谓语用来查找某个特定的节点或者包含某个指定的值的节点。谓语被嵌在方括号中 | 查看这篇文章 |
4. xpath中不要使用tbody标签
tbody标签仅得到所有主流浏览器的部分支持,所以就不要使用了!!!
二、例子
- 例1
网站 http://www.c-c.com/uqyl/u6790719/ 抓取详情时,xpath中不要使用
//div[@class=‘ci-left’]//li[1]/b/text(),而应该使用
//div[@class=‘ci-left’]//li/span[contains(text(),‘联系人’)]/following-sibling::b/text(),使用li[1]这中方式去抓取不是很健壮。
三、博客推荐
- xpath定位中详解id 、starts-with、contains、text()和last() 的用法
- Xpath—following-sibling节点
- xpath使用substring-before()和substring-after()遇到的问题
- XPath函数——字符串函数
四、爬虫规则参考
以下提供的规则在rules_posterity库中
1 页面中显示最大页码
参考:all_wanguoshangwu_b2b3_sell_seeds
mongo搜索命令db.getCollection(‘rules_posterity’).find({’_id’:'all_wanguoshangwu_b2b3_sell_seeds '})
2 有总条数和每页条数
参考:all_wgb2b_wgb2b_sell_seeds
mongo搜索命令:db.getCollection(‘rules_posterity’).find({’_id’:‘all_wgb2b_wgb2b_sell_seeds’})
3 只有 下一页 这种字眼
参考:all_damuzhi_b2b6_sell_seeds
mongo搜索命令:db.getCollection(‘rules_posterity’).find({’_id’:‘all_damuzhi_b2b6_sell_seeds’})