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 相关文章推荐
python3实现暴力穷举博客园密码
Jun 19 Python
使用Python实现从各个子文件夹中复制指定文件的方法
Oct 25 Python
Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)
Feb 05 Python
django中url映射规则和服务端响应顺序的实现
Apr 02 Python
python如何保存文本文件
Jun 07 Python
Python drop方法删除列之inplace参数实例
Jun 27 Python
python 抓取知乎指定回答下视频的方法
Jul 09 Python
六种酷炫Python运行进度条效果的实现代码
Jul 17 Python
完美解决Pycharm中matplotlib画图中文乱码问题
Jan 11 Python
10张动图学会python循环与递归问题
Feb 06 Python
教你如何用Python实现人脸识别(含源代码)
Jun 23 Python
Python 数据可视化之Bokeh详解
Nov 02 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 作用域解析运算符(::)
2010/07/27 PHP
php中关于普通表单多文件上传的处理方法
2011/03/25 PHP
Yii使用Captcha验证码的方法
2015/12/28 PHP
PHP将字符串首字母大小写转换的实例
2017/01/21 PHP
PHP实现多图上传和单图上传功能
2018/05/17 PHP
javascript实现简单的二级联动
2015/03/19 Javascript
JavaScript Length 属性的总结
2015/11/02 Javascript
解析Node.js基于模块和包的代码部署方式
2016/02/16 Javascript
js中获取键盘事件的简单实现方法
2016/10/10 Javascript
Form表单按回车自动提交表单的实现方法
2016/11/18 Javascript
javascript 数组去重复(在线去重工具)
2016/12/17 Javascript
easyUI combobox实现联动效果
2017/01/17 Javascript
基于vue v-for 循环复选框-默认勾选第一个的实现方法
2018/03/03 Javascript
解决vue-router中的query动态传参问题
2018/03/20 Javascript
详解基于webpack&amp;gettext的前端多语言方案
2019/01/29 Javascript
解决vue单页面应用打包后相对路径、绝对路径相关问题
2020/08/14 Javascript
[03:42]2014DOTA2西雅图国际邀请赛 Navi战队巡礼
2014/07/07 DOTA
Python的爬虫包Beautiful Soup中用正则表达式来搜索
2016/01/20 Python
深入讲解Java编程中类的生命周期
2016/02/05 Python
Python中利用Scipy包的SIFT方法进行图片识别的实例教程
2016/06/03 Python
Python实现抓取网页生成Excel文件的方法示例
2017/08/05 Python
python实现跨excel的工作表sheet之间的复制方法
2018/05/03 Python
详解Django-restframework 之频率源码分析
2019/02/27 Python
pyqt 实现为长内容添加滑轮 scrollArea
2019/06/19 Python
python交易记录整合交易类详解
2019/07/03 Python
详解HTML5中download属性的应用
2015/08/06 HTML / CSS
New Balance法国官方网站:购买鞋子和服装
2019/09/01 全球购物
Made in Design英国:设计家具、照明、家庭装饰和花园家具
2019/09/24 全球购物
网友共享的几个面试题关于Java和Unix等方面的
2016/09/08 面试题
物流专业大学生职业生涯规划书范文
2014/01/15 职场文书
《守株待兔》教学反思
2014/03/01 职场文书
优秀管理者事迹材料
2014/05/22 职场文书
信息合作协议书
2014/10/09 职场文书
2015年毕业实习工作总结
2014/12/12 职场文书
大学生自我推荐信范文
2015/03/24 职场文书
读《人生的智慧》有感:闲暇是人生的精华
2019/12/25 职场文书