回顾

之前介绍了Beautifulsoup库,这个库可以让我们不写繁杂的正则表达式就可以爬取数据。但是你可能会觉得Beautifulsoup库不太好用,语法太繁杂,难记。今天介绍一个灵活又强大的网页解析库PyQuery。

什么是PyQuery

如果你熟悉jQuery的语法,那么PyQuery就是爬虫的绝佳选择,api可以无缝迁移。

PyQuery的安装

pip install pyquery

PyQuery的使用

下面案例讲解使用到的都是下面这个字符串

html = '''
<div><ul><li class="item-0">first item</li><li class="item-1"><a href="link2.html">second item</a></li><li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li><li class="item-1 active"><a href="link4.html">fourth item</a></li><li class="item-0"><a href="link5.html">fifth item</a></li></ul></div>
'''
复制代码

(1)字符串初始化

from pyquery import PyQuery as pq
doc = pq(html)#PyQuery对象,直接传入字符串
print(doc('li'))#其实就是css选择器,选择class时前面加‘.’;选择属性时前面加‘#’,选择标签直接写
print(doc('.item-0')[0].text)#输出第一个class值为item-0对应的内容
复制代码

输出:

<li class="item-0">first item</li><li class="item-1">< a href=" ">second item</ a></li><li class="item-0 active">< a href="link3.html"><span class="bold">third item</span></ a></li><li class="item-1 active">< a href="link4.html">fourth item</ a></li><li class="item-0">< a href="link5.html">fifth item</ a></li>first item
复制代码

(2) URL初始化

from pyquery import PyQuery as pq
doc = pq(url='http://www.baidu.com')#直接传入URL,会自动返回请求后的HTML并传入到PyQuery
print(doc('head'))
复制代码

输出:

<head><meta http-equiv="content-type" content="text/html;charset=utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=Edge"/><meta content="always" name="referrer"/><link rel="stylesheet" type="text/css" href="http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css"/><title>ç¾åº¦ä¸ä¸ï¼ä½ å°±ç¥é</title></head>
复制代码

(3)文件初始化

from pyquery import PyQuery as pq
doc = pq(filename='demo.html')#本地文件名
print(doc('li'))
复制代码

基本的CSS选择器

from pyquery import PyQuery as pq
doc = pq(html)
print(doc('#container .list li'))#id前面加‘#’,class选择就在前面加‘.’ 标签的话什么都不加,写在前面就是选择外层元素、后面就是选择里面的元素
复制代码

输出:

<li class="item-0">first item</li>
<li class="item-1">< a href=" ">second item</ a></li>
<li class="item-0 active">< a href="link3.html"><span class="bold">third item</span></ a></li>
<li class="item-1 active">< a href="link4.html">fourth item</ a></li>
<li class="item-0">< a href="link5.html">fifth item</ a></li>
复制代码

(1)查找子元素

from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')
print(type(items))
print(items)
lis = items.find('li')
print(type(lis))
print(lis)
复制代码

输出:

<class 'pyquery.pyquery.PyQuery'>
<ul class="list">
<li class="item-0">first item</li>
<li class="item-1">< a href=" ">second item</ a></li>
<li class="item-0 active">< a href="link3.html"><span class="bold">third item</span></ a></li>
<li class="item-1 active">< a href="link4.html">fourth item</ a></li>
<li class="item-0">< a href="link5.html">fifth item</ a></li>
</ul><class 'pyquery.pyquery.PyQuery'>
<li class="item-0">first item</li>
<li class="item-1">< a href="link2.html">second item</ a></li>
<li class="item-0 active">< a href="link3.html"><span class="bold">third item</span></ a></li>
<li class="item-1 active">< a href="link4.html">fourth item</ a></li>
<li class="item-0">< a href="link5.html">fifth item</ a></li>
复制代码

(2)直接子元素:

lis = items.children(‘.active’)#()中是二次筛选,也可以没有
print(type(lis))
print(lis)
复制代码

(3)父元素

from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')#list的父节点
container = items.parent()
复制代码

输出:

<class 'pyquery.pyquery.PyQuery'>
<div id="container"><ul class="list"><li class="item-0">first item</li><li class="item-1">< a href=" ">second item</ a></li><li class="item-0 active">< a href="link3.html"><span class="bold">third item</span></ a></li><li class="item-1 active">< a href="link4.html">fourth item</ a></li><li class="item-0">< a href="link5.html">fifth item</ a></li></ul></div>
复制代码

返回祖先节点:

from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')
parents = items.parents()
print(type(parents))
print(parents)
复制代码

输出:

<class 'pyquery.pyquery.PyQuery'>
<div class="wrap"><div id="container"><ul class="list"><li class="item-0">first item</li><li class="item-1">< a href=" ">second item</ a></li><li class="item-0 active">< a href="link3.html"><span class="bold">third item</span></ a></li><li class="item-1 active">< a href="link4.html">fourth item</ a></li><li class="item-0">< a href="link5.html">fifth item</ a></li></ul></div></div><div id="container"><ul class="list"><li class="item-0">first item</li><li class="item-1">< a href="link2.html">second item</ a></li><li class="item-0 active">< a href="link3.html"><span class="bold">third item</span></ a></li><li class="item-1 active">< a href="link4.html">fourth item</ a></li><li class="item-0">< a href="link5.html">fifth item</ a></li></ul></div>
复制代码

也可以传入css选择器再次进行筛选:

parent = items.parents('.wrap')
print(parent)
复制代码

只会输出上面的第一个结果

兄弟元素

from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.list .item-0.active')#后面是没有空格,表示查找同时包含这两个class的元素,只有一个符合条件
print(li.siblings())
复制代码

输出的是其他4个兄弟li标签

遍历

from pyquery import PyQuery as pq
doc = pq(html)
lis = doc('li').items()
print(type(lis))
for li in lis:print(li)#每一个li标签都是pyquery类型,可以进行进一步操作
复制代码

获取信息

(1)获取属性值

from pyquery import PyQuery as pq
doc = pq(html)
a = doc('.item-0.active a')
print(a)
print(a.attr('href'))
print(a.attr.href)
复制代码

输出:

< a href=" "><span class="bold">third item</span></ a>
link3.html
link3.html
< a href="link3.html"><span class="bold">third item</span></ a>
link3.html
link3.html
复制代码

(2)获取文本值

from pyquery import PyQuery as pq
doc = pq(html)
a = doc('.item-0.active a')
print(a)
print(a.text())
复制代码

输出:

< a href=" "><span class="bold">third item</span></ a>
third item
复制代码

(3)获取HTML

from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.item-0.active')
print(li)
print(li.html())
复制代码

输出:

<li class="item-0 active">< a href=" "><span class="bold">third item</span></ a></li>
< a href="link3.html"><span class="bold">third item</span></ a>
复制代码

DOM 操作

(1)addClass和removeClass

from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.item-0.active')
print(li)
li.removeClass('active')
print(li)
li.addClass('active')
print(li)
复制代码

输出:

<li class="item-0 active">< a href=" "><span class="bold">third item</span></ a></li><li class="item-0">< a href="link3.html"><span class="bold">third item</span></ a></li><li class="item-0 active">< a href="link3.html"><span class="bold">third item</span></ a></li>
复制代码

DOM操作其实就是对:属性、css、class等进行操作 (2)添加属性attr、添加css

from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.item-0.active')
print(li)
li.attr('name', 'link')#添加新的属性对
print(li)
li.css('font-size', '14px')#添加新的css
print(li)
复制代码

输出:

<li class="item-0 active">< a href=" "><span class="bold">third item</span></ a></li><li class="item-0 active" name="link">< a href="link3.html"><span class="bold">third item</span></ a></li><li class="item-0 active" name="link" style="font-size: 14px">< a href="link3.html"><span class="bold">third item</span></ a></li>
复制代码

(3)移除

html = '''
<div class="wrap">Hello, World<p>This is a paragraph.</p></div>
'''
from pyquery import PyQuery as pq
doc = pq(html)
wrap = doc('.wrap')
print(wrap.text())
wrap.find('p').remove()#单独获取Hello world
print(wrap.text())
复制代码

输出: Hello, World This is a paragraph. Hello, World (5)伪类选择器

from pyquery import PyQuery as pq
doc = pq(html)
li = doc('li:first-child')#获取第一个li标签
print(li)
li = doc('li:last-child')#获取最后一个li标签
print(li)
li = doc('li:nth-child(2)')#获取第二个li标签
print(li)
li = doc('li:gt(2)')#获取大于2的li标签
print(li)
li = doc('li:nth-child(2n)')##获取第偶数个li标签
print(li)
li = doc('li:contains(second)')#获取包含某个文本值的li标签
print(li)
复制代码

(5)其他

  • 其他DOM方法:pyquery.readthedocs.io/en/latest/a…
  • 其他CSS选择器:www.w3school.com.cn/css/index.a…
  • pyquery 官方文档:pyquery.readthedocs.io/

资料分享

java学习笔记、10T资料、100多个java项目分享


欢迎关注个人公众号【菜鸟名企梦】,公众号专注:互联网求职面经javapython爬虫大数据等技术分享**: 公众号**菜鸟名企梦后台发送“csdn”即可免费领取【csdn】和【百度文库】下载服务; 公众号菜鸟名企梦后台发送“资料”:即可领取5T精品学习资料**、java面试考点java面经总结,以及几十个java、大数据项目资料很全,你想找的几乎都有

转载于:https://juejin.im/post/5cbd75c06fb9a03235585d94

【一起学爬虫】PyQuery详解相关推荐

  1. 爬虫解析利器PyQuery详解及使用实践

    作者:叶庭云 整理:Lemon 爬虫解析利器 PyQuery详解及使用实践 之前跟大家分享了 selenium.Scrapy.Pyppeteer 等工具的使用. 今天来分享另一个好用的爬虫解析工具 P ...

  2. python3爬虫实例-python3.7简单的爬虫实例详解

    python3.7简单的爬虫,具体代码如下所示: #https://www.runoob.com/w3cnote/python-spider-intro.html #Python 爬虫介绍 impor ...

  3. python爬虫实例-python3.7简单的爬虫实例详解

    python3.7简单的爬虫,具体代码如下所示: #https://www.runoob.com/w3cnote/python-spider-intro.html #Python 爬虫介绍 impor ...

  4. python selenium爬虫_详解基于python +Selenium的爬虫

    详解基于python +Selenium的爬虫 一.背景 1. Selenium Selenium 是一个用于web应用程序自动化测试的工具,直接运行在浏览器当中,支持chrome.firefox等主 ...

  5. 分享本周所学——Transformer模型详解

    大家好,欢迎来到<分享本周所学>第二期.本人是一名人工智能初学者,最近一周学了一下Transformer这个特别流行而且特别强大的模型,觉得非常有收获,就想用浅显易懂的语言让大家对这个超级 ...

  6. 看漫画学知识:详解获得苹果推荐的4大要素

    随着移动游戏市场的竞争越来越激烈,相对应的推广成本也水涨船高,对于一款app来说,如果能获得苹果编辑的青睐,登上AppStore的推荐位的话,能大幅提升游戏的排名和收入.但是苹果的推荐也不是那么好拿的 ...

  7. 零零散散学算法之详解几种数据存储结构

    影响空间规模的几种数据存储结构 正文 所谓数据存储结构,就是数据的元素与元素之间在计算机中的一种表示,它的目的是为了解决空间规模问题,或者是通过空间规模问题从而间接地解决时间规模问题.我们知道,随着输 ...

  8. 爬虫笔记:pyquery详解

    pyquery 强大又灵活的网页解析库,如果你觉得正则写起来太麻烦,如果你觉得BeautifuiSoup语法太难记,如果你熟悉JQuery的语法,那么PyQuery就是你的绝对选择. 初始化 1字符串 ...

  9. python爬虫知识点总结(七)PyQuery详解

    官方学习文档:http://pyquery.readthedocs.io/en/latest/api.html 一.什么是PyQuery? 答:强大有灵活的网页解析库,模仿jQuery实现.如果你觉得 ...

最新文章

  1. iphone 字符串
  2. Android短信拦截
  3. 资源跳转--response重定向和request转发
  4. 成功解决YOLOv3测试——could not create cudnn handle: CUDNN_STATUS_NOT_INITIALIZE作记录
  5. 西安电子科技大学第16届程序设计竞赛 F题
  6. centos7安装mysql6_2018年第四周-在centos7安装mysql6
  7. UML-基于GRASP对象设计步骤
  8. 出租车计费java_java 计程车计费
  9. AutoCAD2010图边框图.LSP与.DGW文件该放在哪才能正确生成想要的图框
  10. html5font标签菜鸟教程,菜鸟教程
  11. 展望未来城市,万物皆可运营
  12. ROM RAM FLASH说明
  13. word中html在哪,如何word中显示部分的域代码在哪
  14. 李沐动手学深度学习V2-BERT微调和代码实现
  15. Linux- 系统随你玩之--文件管理-双生姐妹花
  16. 关于OLEDB参数化查询【.net】
  17. 编写函数判断一个整数是否为素数
  18. 传感器检测技术及仪表笔记02第二章 检测系统的基本特性
  19. Windows10的系统文件windows.edb文件过大的两种解决办法
  20. RedHat 8 时间同步和时区修改

热门文章

  1. 如何安装python3.8.2_Python 3.8.2详细图文安装教程(附安装包)
  2. 4 安卓安装路径_安卓逆向——APK安装流程
  3. python 做服务程序_windows平台把python程序制作成windows服务并开机启动(实践的坑)...
  4. 网站备案中遇到的问题 名词和解释 大全
  5. Visual Studio 2019报错:缺少mfc120.dll
  6. ubuntu16.04 安装kicad5.1
  7. python输出被五整除的数_python中给定一系列正整数能被5整除的数字中所有偶数的和?...
  8. html事件绑定的方法,如何获取html元素所绑定的事件
  9. c 语言输出字符用什么作用是什么,C语言中输出字符串用什么符号
  10. 计算机专业英语职高 试卷,职高对口高考英语模拟考试题.doc