基础爬虫系列课程授课内容3——xpath语法

19 篇文章 11 订阅
19 篇文章 9 订阅

xpath语法的使用

一、xpath介绍

XPath 是一门在 XML 文档中查找信息的语言。XPath 用于在 XML 文档中通过元素和属性进行导航。

  1. XPath 使用路径表达式在 XML 文档中进行导航
  2. XPath 包含一个标准函数库
  3. XPath 是 XSLT 中的主要元素
  4. XPath 是一个 W3C 标准

请务必清楚
在 XPath 中,有七种类型的节点:元素、属性、文本、命名空间、处理指令、注释以及文档(根)节点。XML 文档是被作为节点树来对待的。

二、xpath语法梳理

在这里插入图片描述
下面举个小例子:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore>

xml.xpath(“bookstore”) 表示选取 bookstore 元素的所有子节点
xml.xpath(“/bookstore”) 表示选取根元素 bookstore。
xml.xpath(“bookstore/book”) 选取属于 bookstore 的子元素的所有 book 元素。
xml.xpath(“//book”) 选取所有 book 子元素,而不管它们在文档中的位置。
xml.xpath(“bookstore//book”) 选择属于 bookstore 元素的后代的所有 book 元素,而不管它们位于 bookstore 之下的什么位置。
xml.xpath(“//@lang”) 选取名为 lang 的所有属性。

谓语
在这里插入图片描述
选取未知节点
在这里插入图片描述
举个例子:
在这里插入图片描述
选取若干路径
通过在路径表达式中使用“|”运算符,您可以选取若干个路径。
//book/title | //book/price 选取 book 元素的所有 title 和 price 元素。
//title | //price 选取文档中的所有 title 和 price 元素。
/bookstore/book/title | //price 选取属于 bookstore 元素的 book 元素的所有 title 元素,以及文档中所有的 price 元素。

三、轴

轴可定义相对于当前节点的节点集。
在这里插入图片描述
步的语法:
轴名称::节点测试[谓语]
举个例子:
在这里插入图片描述

四、常用函数

1、 starts-with函数
获取以xxx开头的元素
例子:xpath(‘//div[stars-with(@class,”test”)]’)

2、contains函数
获取包含xxx的元素
例子:xpath(‘//div[contains(@id,”test”)]’)

3、and
与的关系
例子:xpath(‘//div[contains(@id,”test”) and contains(@id,”title”)]’)

4、 text()函数
例子1:xpath(‘//div[contains(text(),”test”)]’)
例子2:xpath(‘//div[@id=”“test]/text()’)

五、lxml的xpath示例

#coding=utf-8

from lxml import etree

html = '''

<html>
  <head>
    <meta name="content-type" content="text/html; charset=utf-8" />
    <title>友情链接查询 - 站长工具</title>
    <!-- uRj0Ak8VLEPhjWhg3m9z4EjXJwc -->
    <meta name="Keywords" content="友情链接查询" />
    <meta name="Description" content="友情链接查询" />

  </head>
  <body>
    <h1 class="heading">Top News</h1>
    <p style="font-size: 200%">World News only on this page</p>
    Ah, and here's some more text, by the way.
    <p>... and this is a parsed fragment ...</p>

    <a href="http://www.cydf.org.cn/" rel="nofollow" target="_blank">青少年发展基金会</a> 
    <a href="http://www.4399.com/flash/32979.htm" target="_blank">洛克王国</a> 
    <a href="http://www.4399.com/flash/35538.htm" target="_blank">奥拉星</a> 
    <a href="http://game.3533.com/game/" target="_blank">手机游戏</a>
    <a href="http://game.3533.com/tupian/" target="_blank">手机壁纸</a>
    <a href="http://www.4399.com/" target="_blank">4399小游戏</a> 
    <a href="http://www.91wan.com/" target="_blank">91wan游戏</a>

  </body>
</html>

'''
page = etree.HTML(html.lower().decode('utf-8'))
hrefs = page.xpath(u"//a")
for href in hrefs:
    print href.attrib

打印出的结果为:

{‘href’: ‘http://www.cydf.org.cn/‘, ‘target’: ‘_blank’, ‘rel’: ‘nofollow’}
{‘href’: ‘http://www.4399.com/flash/32979.htm‘, ‘target’: ‘_blank’}
{‘href’: ‘http://www.4399.com/flash/35538.htm‘, ‘target’: ‘_blank’}
{‘href’: ‘http://game.3533.com/game/‘, ‘target’: ‘_blank’}
{‘href’: ‘http://game.3533.com/tupian/‘, ‘target’: ‘_blank’}
{‘href’: ‘http://www.4399.com/‘, ‘target’: ‘_blank’}
{‘href’: ‘http://www.91wan.com/‘, ‘target’: ‘_blank’}

如果要获取标签a之间的内容,就可以用print href.text输出。

好啦,xpath语法的基本使用就介绍这么多,谢谢。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值