python爬虫学习笔记之pyquery模块基本用法详解


Posted in Python onApril 09, 2020

本文实例讲述了python爬虫学习笔记之pyquery模块基本用法。分享给大家供大家参考,具体如下:

相关内容:

  • pyquery的介绍
  • pyquery的使用
    • 安装模块
    • 导入模块
    • 解析对象初始化
    • css选择器
    • 在选定元素之后的元素再选取
    • 元素的文本、属性等内容的获取
  • pyquery执行DOM操作、css操作
    • Dom操作
    • CSS操作
  • 一个利用pyquery爬取豆瓣新书的例子

首发时间:2018-03-09 21:26


pyquery的介绍

  • pyquery允许对xml、html文档进行jQuery查询。
  • pyquery使用lxml进行快速xml和html操作。
  • pyquery是python中的jquery

PyQuery的使用:

1.安装模块:

pip3 install pyquery

2.导入模块:

from pyquery import PyQuery as pq

3.解析对象初始化:

【使用PyQuery初始化解析对象,PyQuery是一个类,直接将要解析的对象作为参数传入即可

  • 解析对象为字符串时字符串初始化 :默认情况下是字符串,如果字符串是一个带http\https前缀的,将会认为是一个url
    textParse = pq(html)
  • 解析对象为网页时url初始化: 建议使用关键字参数url=
    # urlParse = pq('http://www.baidu.com') #1
    urlParse = pq(url='http://www.baidu.com') #2
  • 解析对象为文件时文件初始化:建议使用关键字参数filename=
    fileParse = pq(filename="L:\demo.html")
  • 解析完毕后,就可以使用相关函数或变量来进行筛选,可以使用css等来筛选,

4.CSS选择器:

  • 利用标签获取:
    result = textParse('h2').text()
  • 利用类选择器:
    result3=textParse(".p1").text()
  • 利用id选择:
    result4=textParse("#user").attr("type")
  • 分组选择:
    result5=textParse("p,div").text()
  • 后代选择器:
    result6=textParse("div a").attr.href
  • 属性选择器:
    result7=textParse("[class='p1']").text()
  • CSS3伪类选择器:
    result8=textParse("p:last").text()

(更多的,可以参考css)

5.在选定元素之后的元素再选取:

  • find():找出指定子元素 ,find可以有参数,该参数可以是任何 jQuery 选择器的语法,
  • filter():对结果进行过滤,找出指定元素 ,filter可以有参数,该参数可以是任何 jQuery 选择器的语法,
  • children():获取所有子元素,可以有参数,该参数可以是任何 jQuery 选择器的语法,
  • parent():获取父元素,可以有参数,该参数可以是任何 jQuery 选择器的语法,
  • parents():获取祖先元素,可以有参数,该参数可以是任何 jQuery 选择器的语法,
  • siblings():获取兄弟元素,可以有参数,该参数可以是任何 jQuery 选择器的语法,
from pyquery import PyQuery as pq

html="""
<html>
<head>
</head>
<body>
<h2>This is a heading</h2>
<p class="p1">This is a paragraph.</p>
<p class="p2">This is another paragraph.</p>
<div> 
123
<a id="a1" href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" >hello</a>
</div>
<input type="Button" >
<input id="user" type="text" >
</body>
"""

###初始化
textParse = pq(html)
# urlParse = pq('http://www.baidu.com') #1
# urlParse = pq(url='http://www.baidu.com') #2
# fileParse = pq(filename="L:\demo.html")

##获取
result = textParse('h2').text()
print(result)
result2= textParse('div').html()
print(result2)
result3=textParse(".p1").text()
print(result3)
result4=textParse("#user").attr("type")
print(result4)
result5=textParse("p,div").text()
print(result5)
result6=textParse("div a").attr.href
print(result6)
result7=textParse("[class='p1']").text()
print(result7)
result8=textParse("p:last").text()
print(result8)
result9=textParse("div").find("a").text()
print(result9)
result12=textParse("p").filter(".p1").text()
print(result12)
result10=textParse("div").children()
print(result10)
result11=textParse("a").parent()
print(result11)

6.元素的文本、属性等内容的获取:

attr(attribute):获取属性

result2=textParse("a").attr("href")

attr.xxxx:获取属性xxxx

result21=textParse("a").attr.href
result22=textParse("a").attr.class_

text():获取文本,子元素中也仅仅返回文本

result1=textParse("a").text()

html():获取html,功能与text类似,但返回html标签python爬虫学习笔记之pyquery模块基本用法详解

result3=textParse("div").html()

补充1:

元素的迭代:如果返回的结果是多个元素,如果想迭代出每个元素,可以使用items():

python爬虫学习笔记之pyquery模块基本用法详解

补充2:pyquery是jquery的python化,语法基本都是相通的,想了解更多,可以参考jquery。


pyquery执行DOM操作、css操作:

DOM操作:

add_class():增加class

remove_class():移除class

remove():删除指定元素

from pyquery import PyQuery as pq

html="""
<html>
<head>
</head>
<body>
<h2>This is a heading</h2>
<p id="p1" class="p1">This is a paragraph.</p>
<p class="p2">This is another paragraph.</p>
<div style="color:blue"> 
123
<a class="ca" href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" >hello</a>
</div>
<input type="Button" >
<input id="user" type="text" >
</body>
"""

textParse=pq(html)
textParse('a').add_class("c1")
print(textParse('a').attr("class"))

textParse('a').remove_class("c1")
print(textParse('a').attr("class"))

print(textParse('div').html())
textParse('div').remove("a")
print(textParse('div').html())

 

css操作:

  • attr():设置属性
    • 设置格式:attr("属性名","属性值")
  • css():设置css
    • 设置格式1:css("css样式","样式值")
    • 格式2:css({"样式1":"样式值","样式2":"样式值"})
from pyquery import PyQuery as pq

html="""
<html>
<head>
</head>
<body>
<h2>This is a heading</h2>
<p id="p1" class="p1">This is a paragraph.</p>
<p class="p2">This is another paragraph.</p>
<div style="color:blue"> 
123
<a class="ca" href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" >hello</a>
</div>
<input type="Button" >
<input id="user" type="text" >
</body>
"""

textParse=pq(html)
textParse('a').attr("name","hehe")
print(textParse('a').attr("name"))

textParse('a').css("color","white")
textParse('a').css({"background-color":"black","postion":"fixed"})
print(textParse('a').attr("style"))

这些操作什么时候会被用到:

【有时候可能会将数据样式处理一下再存储下来,就需要用到,比如我获取下来的数据样式我不满意,可以自定义成我自己的格式】

【有时候需要逐层清理再筛选出指定结果,比如<div>123<a></a></div>中,如果仅仅想要获取123就可以先删除<a>再获取】


一个利用pyquery爬取豆瓣新书的例子:

先使用审查元素,定位目标元素python爬虫学习笔记之pyquery模块基本用法详解

确认爬取信息python爬虫学习笔记之pyquery模块基本用法详解

要注意的是,豆瓣新书是有一些分在后面页的,实际上目标应该是li的上一级ul:python爬虫学习笔记之pyquery模块基本用法详解

使用PyQuery筛选出结果:

from pyquery import PyQuery as pq

urlParse=pq(url="https://book.douban.com/")

info=urlParse("div.carousel ul li div.info")

file=open("demo.txt","w",encoding="utf8")
for i in info.items():
  title=i.find("div.title")
  author=i.find("span.author")
  abstract=i.find(".abstract")
  file.write("标题:"+title.text()+"\n")
  file.write("作者:"+author.text()+"\n")
  file.write("概要:"+abstract.text()+"\n")
  file.write("-----------------\n")
  print("\n")
file.close()

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python求两个list的差集、交集与并集的方法
Nov 01 Python
SVM基本概念及Python实现代码
Dec 27 Python
Python wxpython模块响应鼠标拖动事件操作示例
Aug 23 Python
python对于requests的封装方法详解
Jan 03 Python
Python pandas用法最全整理
Aug 04 Python
python elasticsearch从创建索引到写入数据的全过程
Aug 04 Python
python list数据等间隔抽取并新建list存储的例子
Nov 27 Python
解决python web项目意外关闭,但占用端口的问题
Dec 17 Python
pycharm通过ssh连接远程服务器教程
Feb 12 Python
Python逐行读取文件内容的方法总结
Feb 14 Python
Python利用matplotlib绘制散点图的新手教程
Nov 05 Python
selenium框架中driver.close()和driver.quit()关闭浏览器
Dec 08 Python
python使用pymongo与MongoDB基本交互操作示例
Apr 09 #Python
使用Python和百度语音识别生成视频字幕的实现
Apr 09 #Python
利用Python制作动态排名图的实现代码
Apr 09 #Python
使用python接受tgam的脑波数据实例
Apr 09 #Python
解决使用python print打印函数返回值多一个None的问题
Apr 09 #Python
Python 实现自动完成A4标签排版打印功能
Apr 09 #Python
python网络编程:socketserver的基本使用方法实例分析
Apr 09 #Python
You might like
深入PHP数据缓存的使用说明
2013/05/10 PHP
一个PHP实现的轻量级简单爬虫
2015/07/08 PHP
使用php从身份证号中获取一系列线索(星座、生肖、生日等)
2016/05/11 PHP
php实现统计二进制中1的个数算法示例
2018/01/23 PHP
小议javascript 设计模式 推荐
2009/10/28 Javascript
用js小类库获取浏览器的高度和宽度信息
2012/01/15 Javascript
浅析LigerUi开发中谨慎载入common.css文件
2013/07/09 Javascript
jQuery中after()方法用法实例
2014/12/25 Javascript
javascript实时显示北京时间的方法
2015/03/12 Javascript
jquery UI Datepicker时间控件的使用方法(终结版)
2015/11/07 Javascript
Bootstrap框架结合jQuery仿百度换肤功能实例解析
2016/09/17 Javascript
JavaScript监听手机物理返回键的两种解决方法
2017/08/14 Javascript
vue组件实践之可搜索下拉框功能
2018/11/25 Javascript
vue实现在v-html的html字符串中绑定事件
2019/10/28 Javascript
JS删除对象中某一属性案例详解
2020/09/08 Javascript
vue-router 控制路由权限的实现
2020/09/24 Javascript
js canvas实现俄罗斯方块
2020/10/11 Javascript
[03:02]辉夜杯主赛事第二日 每日之星
2015/12/27 DOTA
[01:01:31]2018DOTA2亚洲邀请赛3月29日小组赛B组 Mineski VS paiN
2018/03/30 DOTA
[10:05]DOTA2-DPC中国联赛 正赛 iG vs PSG.LGD 选手采访
2021/03/11 DOTA
Mac下Supervisor进程监控管理工具的安装与配置
2014/12/16 Python
Python的Flask框架中Flask-Admin库的简单入门指引
2015/04/07 Python
Python yield 使用浅析
2015/05/28 Python
在python中使用正则表达式查找可嵌套字符串组
2017/10/24 Python
Python Tkinter 简单登录界面的实现
2019/06/14 Python
python多进程(加入进程池)操作常见案例
2019/10/21 Python
浅谈Pytorch中的自动求导函数backward()所需参数的含义
2020/02/29 Python
Python图像识别+KNN求解数独的实现
2020/11/13 Python
python爬虫调度器用法及实例代码
2020/11/30 Python
Python爬虫回测股票的实例讲解
2021/01/22 Python
SQL SERVER面试资料
2013/03/30 面试题
煤矿开采专业求职信
2014/07/08 职场文书
健康状况证明书
2014/11/26 职场文书
入党积极分子考察意见
2015/06/02 职场文书
《植物妈妈有办法》教学反思
2016/02/23 职场文书
解决python存数据库速度太慢的问题
2021/04/23 Python