Python爬虫-Pyquery的用法(四)
一、 PyQuery介绍与安装
1、PyQuery简介
PyQuery简介
- PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择。
-
- 官网地址:http://pyquery.readthedocs.io/en/latest/
- jQuery参考文档:可以用来查找选择器
2、PyQuery的安装
1 2 | pip install lxml #pyquery依赖于lxml解析库 pip install pyquery |
二、 PyQuery的使用
1、初始化工作(会自动补齐不完整html标签)
在解析 HTML 文本的时候,首先需要将其初始化为一个 pyquery 对象。PyQuery的初始化方式有多种:传入字符串、 URL、文件名,等等。
1.1、 字符串初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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> '' ' from pyquery import PyQuery as pq doc = pq(html) print(doc( 'li' )) |
1.2、URL 初始化(初始化的参数还可以传入网页的 URL,此时只需要指定参数为 url 即可)
1 2 3 | from pyquery import PyQuery as pq doc = pq(url= 'http://www.baidu.com' ,encoding= "utf-8" ) print(doc( 'title' )) |
1.3、注意:初始化URL时推荐先使用爬虫库(urllib、resquests)封装URL后再传递给pyquery(爬取数据和解析数据分开):
1 2 3 4 5 6 7 | from pyquery import PyQuery as pq import requests res = requests. get ( 'http://www.baidu.com' ) res.encoding= "utf-8" doc = pq(res.text) print(doc( 'title' )) |
1.4、文件初始化(传递一个本地的文件名,参数指定为 filename 即可)
demo.html文件示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <div id= "container" > <ul class = "list" > <li class = "item-0" >first item</li> <li class = "item-1" ><a rel= "nofollow" href= "link2.html" >second item</a></li> <li id= "item-0" ><a rel= "nofollow" href= "link2.html" >second item</a></li> <li class = "item-0 active" ><a rel= "nofollow" href= "link3.html" ><span class = "bold" >third item</span></a></li> <li class = "item-1 active" ><a rel= "nofollow" href= "link4.html" >fourth item</a></li> <li class = "shop" ><a rel= "nofollow" href= "link5.html" >fifth item</a></li> <a rel= "nofollow" href= "link5.html" >te xxx</a> <p>tet1</p> <p>tet2</p> <p>tet3</p> </ul> <div>test0001</div> </div> |
1 2 3 | from pyquery import PyQuery as pq doc = pq(filename= 'demo.html' ) print(doc( 'li' )) |
2、查找节点
2.1、直接查找节点名
语法:doc("节点名")
——通过CSS选择器来获取目标内容(满足CSS语法)。
1 2 3 4 5 | # 选取class为list的节点 items = doc( '.list' ) #关联选择符的使用 print(doc( "ul li.shop a" )) |
2.2、使用函数查找嵌套元素(等价于:doc("")
,嵌套元素用空格隔开)
- 语法:
doc("节点名").find("节点名")
- 注意:find 的查找范围是节点的所有子孙节点,而如果我们只想查找子节点,那可以用 children 方法。
1 2 3 4 5 6 7 | #查找 <ul> 内部的 a 元素 print(doc( 'ul' ).find( 'a' )) print(doc( "ul a" )) #查找 <ul> 内的直接子 a 子标签 print(doc( 'ul' ).children( 'a' )) print(doc( "ul > a" )) |
2.3、根据 class、id 筛选指定元素。
- 语法:
doc("节点名").filter()
1 2 3 4 5 6 7 | #查找 class 为 item-1 的 li 元素() print(doc( 'li' ).filter( '.item-1' )) print(doc( "li.item-1" )) #查找 id 为 item-0 的 li元素 print(doc( 'li' ).filter( '#item-0' )) print(doc( "li#item-0" ) |
3、遍历
pyquery的选择结果可能是单个节点(可以直接打印输出,也可以直接转成字符串);
也可能是多个节点,类型都是PyQuery类型,多个结果时所以需要遍历来获取。
这时候需要调用 items() 方法;调用 items 方法后,会得到一个生成器,遍历一下,就可以逐个得到 单节点对象了。
语法:doc("节点名").items()
1 2 3 | lis = doc( 'li' ).items() for li in lis: print(li) |
4、获取信息、修改信息
提取到节点之后,我们的最终目的当然是提取节点所包含的信息了。比较重要的信息有两类,一是获取属性,二是获取文本。
4.1、获取、修改文本信息
语法:
doc("节点名").html()——用html()获取HTML文本;
doc("节点名").text()——用text()来获取文本(只返回纯文字内容)。
1 2 | print(doc( 'a' ).text()) #返回所有a标签文本内容 print(doc( 'a' ).text( "阿里" )) #会将所有a标签内容修改为“阿里” |
5、获取祖先节点、子孙节点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #获取祖先节点 # 子节点 items = doc( '.list' ) lis = items.children() print(lis) # class为active的子节点 lis = items.children( '.active' ) print(lis) # 父节点 lis = items.parent() print(lis) # 祖先节点,如果需要筛选,也可以传入CSS选择器 container = items.parents() print(container) # 兄弟节点 lis1 =items.siblings() print(lis1) |
6、节点操作
pyquery 提供了一系列方法来对节点进行动态修改,比如为某个节点添加一个 class,移除某个节点等,这些操作有时会为提取信息带来极大的便利。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | li = doc( 'item-0.active' ) #获取元素属性的值 li.attr( 'href' ) # 删除class属性 li.removeClass( 'active' ) # 添加class属性 li.addClass( 'active' ) # 改变属性 li.attr( 'name' , 'link' ) # 改变文本内容 li.text(str) # 改变html li.html(html) # 移除指定节点 li.remove() li.find( "a" ).remove() #移除子节点a标签 |
7、伪类选择器
1 2 3 4 5 6 7 8 9 10 11 12 | # 第一个li节点 li = doc( 'li:first-hild' ) # 最后一个li节点 li = doc( 'li:last-child' ) # 第二个li节点 li = doc( 'li:nth-child(2)' ) # 第三个li之后的li节点 li = doc( 'li:gt(2)' ) # 偶数位置的li节点 li = doc( 'li:nth-child(2n)' ) # 包含second文本的li节点 li = doc( 'li:contains(second)' ) |
本文来自博客园,作者:橘子偏爱橙子,转载请注明原文链接:https://www.cnblogs.com/xfbk/p/16649447.html
分类:
python爬虫
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构