1. # 字符串初始化
  2. html = '''
  3. <div>
  4. <ul>
  5. <li class = "item-0">first item</li>
  6. <li class = "item-1"><a href = "link2.html">second item</a></li>
  7. <li class = "item-0 active"><a href = "link3.html"><span class = "bold">third item</span></a></li>
  8. <li class = "item-1 active"><a href = "link4.html">fourth item</a></li>
  9. <li class = "item-0"><a href = "link5.html">fifthth item</a></li>
  10. </ul>
  11. </div>
  12. '''
  13.  
  14. from pyquery import PyQuery as pq
  15. doc = pq(html)
  16. print(doc('li'))
  17.  
  18. # url初始化
  19. from pyquery import PyQuery as pq
  20. doc = pq(url = "http://www.baidu.com")
  21. print(doc("head"))
  22.  
  23. # 文件初始化
  24. from pyquery import PyQuery as pq
  25. doc = pq(filename = "demo.html")
  26. print(doc('li'))
  27.  
  28. # 基本CSS选择器
  29. html = '''
  30. <div id = "container">
  31. <ul class = "list">
  32. <li class = "item-0">first item</li>
  33. <li class = "item-1"><a href = "link2.html">second item</a></li>
  34. <li class = "item-0 active"><a href = "link3.html"><span class = "bold">third item</span></a></li>
  35. <li class = "item-1 active"><a href = "link4.html">fourth item</a></li>
  36. <li class = "item-0"><a href = "link5.html">fifthth item</a></li>
  37. </ul>
  38. </div>
  39. '''
  40. from pyquery import PyQuery as pq
  41. doc = pq(html)
  42. # 注意下面id 前面需要加上#,class 前面需要加上.
  43. print(doc('#container .list li'))
  44.  
  45. # 查找元素
  46. # 子元素
  47. html = '''
  48. <div id = "container">
  49. <ul class = "list">
  50. <li class = "item-0">first item</li>
  51. <li class = "item-1"><a href = "link2.html">second item</a></li>
  52. <li class = "item-0 active"><a href = "link3.html"><span class = "bold">third item</span></a></li>
  53. <li class = "item-1 active"><a href = "link4.html">fourth item</a></li>
  54. <li class = "item-0"><a href = "link5.html">fifthth item</a></li>
  55. </ul>
  56. </div>
  57. '''
  58. from pyquery import PyQuery as pq
  59. doc = pq(html)
  60. items = doc('.list')
  61. print(type(items))
  62. print(items)
  63. lis = items.find('li')
  64. print(type(lis))
  65. print(lis)
  66.  
  67. lis = items.children()
  68. print(type(lis))
  69. print(lis)
  70.  
  71. lis = items.children('.active')
  72. print(lis)
  73.  
  74. # 父元素
  75. html = '''
  76. <div id = "container">
  77. <ul class = "list">
  78. <li class = "item-0">first item</li>
  79. <li class = "item-1"><a href = "link2.html">second item</a></li>
  80. <li class = "item-0 active"><a href = "link3.html"><span class = "bold">third item</span></a></li>
  81. <li class = "item-1 active"><a href = "link4.html">fourth item</a></li>
  82. <li class = "item-0"><a href = "link5.html">fifthth item</a></li>
  83. </ul>
  84. </div>
  85. '''
  86. from pyquery import PyQuery as pq
  87. doc = pq(html)
  88. items = doc('.list')
  89. container = items.parent()
  90. print(type(container))
  91. print(container)
  92.  
  93. html = '''
  94. <div class = "wrap">
  95. <div id = "container">
  96. <ul class = "list">
  97. <li class = "item-0">first item</li>
  98. <li class = "item-1"><a href = "link2.html">second item</a></li>
  99. <li class = "item-0 active"><a href = "link3.html"><span class = "bold">third item</span></a></li>
  100. <li class = "item-1 active"><a href = "link4.html">fourth item</a></li>
  101. <li class = "item-0"><a href = "link5.html">fifthth item</a></li>
  102. </ul>
  103. </div>
  104. </div>
  105. '''
  106. from pyquery import PyQuery as pq
  107. doc = pq(html)
  108. items = doc('.list')
  109. parents = items.parents()
  110. print(type(parents))
  111. print(parents)
  112.  
  113. parents = items.parents('.wrap')
  114. print(parents)
  1. # 兄弟元素
  2. html = '''
  3. <div class = "wrap">
  4. <div id = "container">
  5. <ul class = "list">
  6. <li class = "item-0">first item</li>
  7. <li class = "item-1"><a href = "link2.html">second item</a></li>
  8. <li class = "item-0 active"><a href = "link3.html"><span class = "bold">third item</span></a></li>
  9. <li class = "item-1 active"><a href = "link4.html">fourth item</a></li>
  10. <li class = "item-0"><a href = "link5.html">fifthth item</a></li>
  11. </ul>
  12. </div>
  13. </div>
  14. '''
  15. from pyquery import PyQuery as pq
  16. doc = pq(html)
  17. # 注意下面item-0后面直接是. 没有空格
  18. li = doc('.list .item-0.active')
  19. print(li.siblings())
  20.  
  21. print(li.siblings('.active'))
  22.  
  23. # 遍历
  24. # 单个元素
  25. html = '''
  26. <div class = "wrap">
  27. <div id = "container">
  28. <ul class = "list">
  29. <li class = "item-0">first item</li>
  30. <li class = "item-1"><a href = "link2.html">second item</a></li>
  31. <li class = "item-0 active"><a href = "link3.html"><span class = "bold">third item</span></a></li>
  32. <li class = "item-1 active"><a href = "link4.html">fourth item</a></li>
  33. <li class = "item-0"><a href = "link5.html">fifthth item</a></li>
  34. </ul>
  35. </div>
  36. </div>
  37. '''
  38. from pyquery import PyQuery as pq
  39. doc = pq(html)
  40. li = doc('.item-0.active')
  41. print(li)
  42.  
  43. html = '''
  44. <div class = "wrap">
  45. <div id = "container">
  46. <ul class = "list">
  47. <li class = "item-0">first item</li>
  48. <li class = "item-1"><a href = "link2.html">second item</a></li>
  49. <li class = "item-0 active"><a href = "link3.html"><span class = "bold">third item</span></a></li>
  50. <li class = "item-1 active"><a href = "link4.html">fourth item</a></li>
  51. <li class = "item-0"><a href = "link5.html">fifthth item</a></li>
  52. </ul>
  53. </div>
  54. </div>
  55. '''
  56. from pyquery import PyQuery as pq
  57. doc = pq(html)
  58. lis = doc('li').items()
  59. print(type(lis))
  60. for li in lis:
  61. print(li)
  62.  
  63. # 获取信息
  64. # 获取属性
  65. html = '''
  66. <div class = "wrap">
  67. <div id = "container">
  68. <ul class = "list">
  69. <li class = "item-0">first item</li>
  70. <li class = "item-1"><a href = "link2.html">second item</a></li>
  71. <li class = "item-0 active"><a href = "link3.html"><span class = "bold">third item</span></a></li>
  72. <li class = "item-1 active"><a href = "link4.html">fourth item</a></li>
  73. <li class = "item-0"><a href = "link5.html">fifthth item</a></li>
  74. </ul>
  75. </div>
  76. </div>
  77. '''
  78. from pyquery import PyQuery as pq
  79. doc = pq(html)
  80. a = doc('.item-0.active a')
  81. print(a)
  82. # 获取属性的两种方法
  83. print(a.attr('href'))
  84. print(a.attr.href)
  85.  
  86. # 获取文本
  87. print(a.text())
  88.  
  89. # 获取html
  90. from pyquery import PyQuery as pq
  91. doc = pq(html)
  92. li = doc('.item-0.active')
  93. print(li)
  94. # 得到<li>标签里面的代码
  95. print(li.html())
  96.  
  97. # DOM操作
  98. # addClass、removeClass
  99. from pyquery import PyQuery as pq
  100. doc = pq(html)
  101. li = doc('.item-0.active')
  102. print(li)
  103. li.remove_class('active')
  104. print(li)
  105. li.add_class('active')
  106. print(li)
  107.  
  108. # attr CSS
  109. li.attr('name', 'link')
  110. print(li)
  111. li.css('font-size', '14px')
  112. print(li)
  113.  
  114. # remove
  115. html = '''
  116. <div class = "wrap">
  117. Hello,World
  118. <p>This is a paragraph</p>
  119. </div>
  120. '''
  121. from pyquery import PyQuery as pq
  122. doc = pq(html)
  123. wrap = doc('.wrap')
  124. print(wrap.text())
  125. wrap.find('p').remove()
  126. print(wrap.text())
  127.  
  128. # 伪类选择器
  129. html = '''
  130. <div class = "wrap">
  131. <div id = "container">
  132. <ul class = "list">
  133. <li class = "item-0">first item</li>
  134. <li class = "item-1"><a href = "link2.html">second item</a></li>
  135. <li class = "item-0 active"><a href = "link3.html"><span class = "bold">third item</span></a></li>
  136. <li class = "item-1 active"><a href = "link4.html">fourth item</a></li>
  137. <li class = "item-0"><a href = "link5.html">fifthth item</a></li>
  138. </ul>
  139. </div>
  140. </div>
  141. '''
  142. from pyquery import PyQuery as pq
  143. doc = pq(html)
  144. # 获取第一个元素
  145. li = doc('li:first-child')
  146. print(li)
  147. # 获取最后一个元素
  148. li = doc('li:last-child')
  149. print(li)
  150. # 获取第二个元素
  151. li = doc('li:nth-child(2)')
  152. print(li)
  153. # 获取下标为2的元素后面的所有元素(下标从0开始)
  154. li = doc('li:gt(2)')
  155. print(li)
  156. # 获取下标为偶数的元素
  157. li = doc('li:nth-child(2n)')
  158. print(li)
  159. # 获取内容包含second 的元素
  160. li = doc('li:contains(second)')
  161. print(li)

Python爬虫之pyquery库的基本使用的更多相关文章

  1. Python爬虫之PyQuery使用(六)

    Python爬虫之PyQuery使用 PyQuery简介 pyquery能够通过选择器精确定位 DOM 树中的目标并进行操作.pyquery相当于jQuery的python实现,可以用于解析HTML网 ...

  2. python爬虫之urllib库(三)

    python爬虫之urllib库(三) urllib库 访问网页都是通过HTTP协议进行的,而HTTP协议是一种无状态的协议,即记不住来者何人.举个栗子,天猫上买东西,需要先登录天猫账号进入主页,再去 ...

  3. python爬虫之urllib库(二)

    python爬虫之urllib库(二) urllib库 超时设置 网页长时间无法响应的,系统会判断网页超时,无法打开网页.对于爬虫而言,我们作为网页的访问者,不能一直等着服务器给我们返回错误信息,耗费 ...

  4. python爬虫之urllib库(一)

    python爬虫之urllib库(一) urllib库 urllib库是python提供的一种用于操作URL的模块,python2中是urllib和urllib2两个库文件,python3中整合在了u ...

  5. Python爬虫之selenium库使用详解

    Python爬虫之selenium库使用详解 本章内容如下: 什么是Selenium selenium基本使用 声明浏览器对象 访问页面 查找元素 多个元素查找 元素交互操作 交互动作 执行JavaS ...

  6. Mac os 下 python爬虫相关的库和软件的安装

      由于最近正在放暑假,所以就自己开始学习python中有关爬虫的技术,因为发现其中需要安装许多库与软件所以就在这里记录一下以避免大家在安装时遇到一些不必要的坑. 一. 相关软件的安装:   1. h ...

  7. python爬虫(四)_urllib2库的基本使用

    本篇我们将开始学习如何进行网页抓取,更多内容请参考:python学习指南 urllib2库的基本使用 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地.在Python中有很 ...

  8. python爬虫之PyQuery的基本使用

    PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严 ...

  9. python爬虫之requests库

    在python爬虫中,要想获取url的原网页,就要用到众所周知的强大好用的requests库,在2018年python文档年度总结中,requests库使用率排行第一,接下来就开始简单的使用reque ...

随机推荐

  1. 你还在 Select * 吗?

    应用程序慢如牛,原因多多,可能是网络的原因.可能是系统架构的原因,还有可能是数据库的原因. 那么如何提高数据库SQL语句执行速度呢?有人会说性能调优是数据库管理员(DBA)的事,然而性能调优跟程序员们 ...

  2. 使用xUnit为.net core程序进行单元测试 -- Assert

    第一部分: http://www.cnblogs.com/cgzl/p/8283610.html Assert Assert做什么?Assert基于代码的返回值.对象的最终状态.事件是否发生等情况来评 ...

  3. Django+Bootstrap+Mysql 搭建个人博客(二)

    2.1.博客首页设计 (1)settings.py MEDIA_ROOT = os.path.join(BASE_DIR,'media').replace("//","/ ...

  4. Kibana(一张图片胜过千万行日志)

    Kibana是一个开源的分析和可视化平台,设计用于和Elasticsearch一起工作. 你用Kibana来搜索,查看,并和存储在Elasticsearch索引中的数据进行交互. 你可以轻松地执行高级 ...

  5. RabbitMQ学习笔记(六) RPC

    什么RPC? 这一段是从度娘摘抄的. RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的 ...

  6. Lucene 06 - 使用Lucene的Query API查询数据

    目录 1 Query对象的创建(方式一): 使用子类对象 1.1 常用的Query子类对象 1.2 常用的Query子类对象使用 1.2.1 使用TermQuery 1.2.2 使用NumericRa ...

  7. SpringCloud应对高并发的思路

    一.Eureka的高可用性 Eureka下面的服务实例默认每隔30秒会发送一个HTTP心跳给Eureka,来告诉Eureka服务还活着,每个服务实例每隔30秒也会通过HTTP请求向Eureka获取服务 ...

  8. Spring Boot(六)集成 MyBatis 操作 MySQL 8

    一.简介 1.1 MyBatis介绍 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC代码和手动设置参数以及获取结果集. ...

  9. 四种途径提高RabbitMQ传输数据的可靠性(二)

    前言 上一篇四种途径提高RabbitMQ传输消息数据的可靠性(一)已经介绍了两种方式提高数据可靠性传输的方法,本篇针对上一篇中提出的问题(1)与问题(2)提出解决常用的方法. 本文其实也就是结合以上四 ...

  10. GitHub 开源的 MySQL 在线更改 Schema 工具【转】

    本文来自:https://segmentfault.com/a/1190000006158503 原文:gh-ost: GitHub's online schema migration tool fo ...