当前位置 博文首页 > Python爬虫必备之XPath解析库

    Python爬虫必备之XPath解析库

    作者:红豆窦 时间:2021-08-09 18:40

    目录
    • 一、简介
    • 二、安装
    • 三、节点
      • 3.1 选取节点
      • 3.2 选取未知节点
      • 3.3 节点关系
    • 四、XPath实例

      一、简介

      XPath 是一门在 XML 文档中查找信息的语言。XPath 可用来在 XML 文档中对元素和属性进行遍历。XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointer 都构建于 XPath 表达之上。

      Xpath解析库介绍:数据解析的过程中使用过正则表达式, 但正则表达式想要进准匹配难度较高, 一旦正则表达式书写错误, 匹配的数据也会出错。

      网页由三部分组成: HTML, Css, JavaScript, HTML页面标签存在层级关系, 即DOM树, 在获取目标数据时可以根据网页层次关系定位标签, 在获取标签的文本或属性。

      二、安装

      pip install lxml

      三、节点

      3.1 选取节点

      XPath 使用路径表达式在 XML 文档中选取节点。节点是通过沿着路径或者 step 来选取的。 下面列出了最有用的路径表达式:

      表达式 描述
      nodename 选取此节点的所有子节点。
      / 从根节点选取。
      // 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
      选取当前节点的父节点。
      . 选取当前节点。
      @ 选取属性。

      3.2 选取未知节点

      XPath 通配符可用来选取未知的 XML 元素。

      通配符 描述
      * 匹配任何元素节点。
      @* 匹配任何属性节点。
      node() 匹配任何类型的节点。

      在下面的表格中,我们列出了一些路径表达式,以及这些表达式的结果:

      路径表达式 结果
      /bookstore/* 选取 bookstore 元素的所有子元素。
      //* 选取文档中的所有元素。
      //title[@*] 选取所有带有属性的 title 元素。

      3.3 节点关系

      父(Parent)

      每个元素以及属性都有一个父。
      在下面的例子中,book 元素是 title、author、year 以及 price 元素的父:

      <book>
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
      </book>

      子(Children)

      元素节点可有零个、一个或多个子。
      在下面的例子中,title、author、year 以及 price 元素都是 book 元素的子:

      <book>
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
      </book>

      同胞(Sibling)

      拥有相同的父的节点
      在下面的例子中,title、author、year 以及 price 元素都是同胞:

      <book>
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
      </book>

      先辈(Ancestor)

      某节点的父、父的父,等等。
      在下面的例子中,title 元素的先辈是 book 元素和 bookstore 元素:

      <bookstore>
      
      <book>
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
      </book>
      
      </bookstore>
      

      后代(Descendant)

      某个节点的子,子的子,等等。
      在下面的例子中,bookstore 的后代是 book、title、author、year 以及 price 元素:

      <bookstore>
      
      <book>
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
      </book>
      
      </bookstore>
      

      四、XPath实例

      爬取糗事百科

      import requests
      # 导包
      from lxml import etree
      import os
      base_url = 'https://www.qiushibaike.com/video/'
      headers = {
          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
      }
      res = requests.get(url=base_url, headers=headers)
      html = res.content.decode('utf-8')
      # xpath解析
      tree = etree.HTML(html)
      # 标题
      content = tree.xpath('//*/a/div[@class="content"]/span/text()')
      # 视频
      video_list = tree.xpath('//*/video[@controls="controls"]/source/@src')
      index = 0
      for i in video_list:
          # 获取视频二进制流
          video_content = requests.get(url= 'https:' + i,headers=headers).content
          # 标题
          title_1 = content[0].strip('\n')
          # 将视频二进制写入文件
          with open(f'Video/{title_1}.mp4','wb') as f:
              f.write(video_content)
          index += 1
      
      jsjbwy
      下一篇:没有了