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

目录

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

      【运行结果】
      test02.html

      URL URL以CSDN首页地址为例:
      from pyquery import PyQuery as pqdoc = pq(url = 'https://www.csdn.net/')print(doc('title'))

      【运行结果】
      CSDN - 专业开发者社区


      文件初始化 我们将以下字符串保存为一个HTML文件,通过文件的形式进行初始化。
      【test02.html】
      Harry Potter29.99Learning XML39.95

      from pyquery import PyQuery as pqdoc = pq(filename = 'test02.html')print(doc('title'))

      【运行结果】
      Harry Potter
      Learning XML

      2、查找节点

      (1)查找子节点 查找子节点时需要用到find()方法,此时传入的参数是CSS选择器。
      from pyquery import PyQuery as pqdoc = pq(filename = 'test02.html')item = doc('book')print(item)lis1 = item.find('title')lis2 = item.find('price')print(lis1)print(lis2)

      【运行结果】

      Harry Potter
      29.99


      Learning XML
      39.95

      Harry Potter
      Learning XML

      29.99
      39.95
      可以看到,我们首先匹配的是book节点,然后匹配book节点下的子节点title和price。
      其实使用find方法匹配的是所有的子孙节点,如果只是单纯匹配子节点可以使用children方法。

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

      (3)匹配兄弟节点 可以使用siblings()方法。

      3、遍历
      对于获取到的内容如果是单个节点,则可以直接转换为字符串类型,而对于获取到多个节点,因其类型为PyQuery类型,需要对获取到的数据进行遍历,这是需要调用items()方法。
      from pyquery import PyQuery as pqdoc = pq(filename = 'test02.html')items = doc('title').items()print(items)print(type(items))for i in items:print(type(i))print(i)

      【运行结果】



      Harry Potter


      Learning XML

      4、获取信息

      (1)获取属性 使用attr()方法
      from pyquery import PyQuery as pqdoc = 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 pqdoc = 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 pqdoc = pq(filename = 'test02.html')items = doc('title')for i in items.items():print(i)i.addClass('book01')print(i)i.removeClass('book01')print(i)

      【运行结果】
      Harry Potter

      Harry Potter

      Harry Potter

      Learning XML

      Learning XML

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

      (2)attr、text、html attr:用来改变属性值;
      text:用来改变文本值;
      html:用来改变节点值;

      (3)remove 移除不需要的节点值,将整个节点移除。

      6、伪类选择器
      支持多种伪类选择器,例如选择第一个节点、最后一个节点、奇数节点、偶数节点、以及包含指定文本的节点等。
      【python网络爬虫精解之pyquery的使用说明】到此这篇关于python网络爬虫精解之pyquery的使用说明的文章就介绍到这了,更多相关python pyquery 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

        推荐阅读