python网络爬虫精解之pyquery的使用说明

2021-09-27,

目录
  • 一、pyquery的介绍
  • 二、pyquery的使用
    • 1、初始化工作
      • 字符串
      • URL
      • 文件初始化
    • 2、查找节点
      • (1)查找子节点
      • (2)匹配父节点
      • (3)匹配兄弟节点
    • 3、遍历
      • 4、获取信息
        • (1)获取属性
        • (2)获取文本
      • 5、节点操作
        • (1)为某个节点添加或删除一个class
        • (2)attr、text、html
        • (3)remove
      • 6、伪类选择器

      pyquery的使用

      一、pyquery的介绍

      使用pyquery需要在Web和了解jQuery的基础上,使用该CSS选择器。

      二、pyquery的使用

      1、初始化工作

      使用pyquery初始化的方式有很多,传入的参数可以是字符串,也可以是URL和文件名,下面将一一介绍初始化方法。

      字符串

      html = '''
      <html>
      
      <head>
        <meta charset="utf-8">
        <title>test02.html</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
      </head>
      
      <body>
        <div id="container">
          <iframe id="iframe" sandbox="allow-scripts" src="/files/%E7%88%AC%E8%99%AB%E5%86%99%E4%BD%9C%E4%BB%A3%E7%A0%81%E6%B5%8B%E8%AF%95/test02.html"></iframe>
        </div>
      </body>
      
      </html>
      '''
      from pyquery import PyQuery as pq
      doc = pq(html)
      print(doc('title'))
      

      【运行结果】

      <title>test02.html</title>

      URL

      URL以CSDN首页地址为例:

      from pyquery import PyQuery as pq
      doc = pq(url = 'https://www.csdn.net/')
      print(doc('title'))
      

      【运行结果】

      <title>CSDN - 专业开发者社区</title>

      文件初始化

      我们将以下字符串保存为一个HTML文件,通过文件的形式进行初始化。

      【test02.html】

      <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>
      
      from pyquery import PyQuery as pq
      doc = pq(filename = 'test02.html')
      print(doc('title'))
      

      【运行结果】

      <title lang="eng">Harry Potter</title>
      <title lang="eng">Learning XML</title>

      2、查找节点

      (1)查找子节点

      查找子节点时需要用到find()方法,此时传入的参数是CSS选择器。

      from pyquery import PyQuery as pq
      doc = pq(filename = 'test02.html')
      item = doc('book')
      print(item)
      lis1 = item.find('title')
      lis2 = item.find('price')
      print(lis1)
      print(lis2)
      

      【运行结果】

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

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

      <title lang="eng">Harry Potter</title>
      <title lang="eng">Learning XML</title>

      <price>29.99</price>
      <price>39.95</price>
      可以看到,我们首先匹配的是book节点,然后匹配book节点下的子节点title和price。

      其实使用find方法匹配的是所有的子孙节点,如果只是单纯匹配子节点可以使用children方法。

      (2)匹配父节点

      使用parent()方法,如果是要匹配祖先节点,则需要使用parents()方法。

      (3)匹配兄弟节点

      可以使用siblings()方法。

      3、遍历

      对于获取到的内容如果是单个节点,则可以直接转换为字符串类型,而对于获取到多个节点,因其类型为PyQuery类型,需要对获取到的数据进行遍历,这是需要调用items()方法。

      from pyquery import PyQuery as pq
      doc = pq(filename = 'test02.html')
      items = doc('title').items()
      print(items)
      print(type(items))
      for i in items:
          print(type(i))
          print(i)
          
      

      【运行结果】

      <generator object PyQuery.items at 0x000002B79E13EF48>
      <class 'generator'>
      <class 'pyquery.pyquery.PyQuery'>
      <title lang="eng">Harry Potter</title>

      <class 'pyquery.pyquery.PyQuery'>
      <title lang="eng">Learning XML</title>

      4、获取信息

      (1)获取属性

      使用attr()方法

      from pyquery import PyQuery as pq
      doc = pq(filename = 'test02.html')
      items = doc('title')
      for i in items.items():
          print(i.attr('lang'))   
      

      【运行结果】

      eng
      eng

      遍历获取到的数据,就能获得所有title节点的land属性值。

      (2)获取文本

      使用text()方法

      from pyquery import PyQuery as pq
      doc = pq(filename = 'test02.html')
      items = doc('title')
      for i in items.items():
          print(i.text())  
      

      【运行结果】

      Harry Potter
      Learning XML

      同样是遍历,获取到每一个title节点的文本值。

      5、节点操作

      (1)为某个节点添加或删除一个class

      调用的方法为addClass和removeClass

      from pyquery import PyQuery as pq
      doc = pq(filename = 'test02.html')
      items = doc('title')
      for i in items.items():
          print(i)
          i.addClass('book01')
          print(i)
          i.removeClass('book01')
          print(i)
      

      【运行结果】

      <title lang="eng">Harry Potter</title>

      <title lang="eng" class="book01">Harry Potter</title>

      <title lang="eng" class="">Harry Potter</title>

      <title lang="eng">Learning XML</title>

      <title lang="eng" class="book01">Learning XML</title>

      <title lang="eng" class="">Learning XML</title>

      可以看到,首先是打印最初始的title节点,加上class属性后再次打印,去掉class属性后再次打印。

      (2)attr、text、html

      attr:用来改变属性值;

      text:用来改变文本值;

      html:用来改变节点值;

      (3)remove

      移除不需要的节点值,将整个节点移除。

      6、伪类选择器

      支持多种伪类选择器,例如选择第一个节点、最后一个节点、奇数节点、偶数节点、以及包含指定文本的节点等。

      到此这篇关于python网络爬虫精解之pyquery的使用说明的文章就介绍到这了,更多相关python pyquery 内容请搜索北冥有鱼以前的文章或继续浏览下面的相关文章希望大家以后多多支持北冥有鱼!

      《python网络爬虫精解之pyquery的使用说明.doc》

      下载本文的Word格式文档,以方便收藏与打印。