Python爬虫程序设计KC23.pptx
- 【下载声明】
1. 本站全部试题类文档,若标题没写含答案,则无答案;标题注明含答案的文档,主观题也可能无答案。请谨慎下单,一旦售出,不予退换。
2. 本站全部PPT文档均不含视频和音频,PPT中出现的音频或视频标识(或文字)仅表示流程,实际无音频或视频文件。请谨慎下单,一旦售出,不予退换。
3. 本页资料《Python爬虫程序设计KC23.pptx》由用户(三亚风情)主动上传,其收益全归该用户。163文库仅提供信息存储空间,仅对该用户上传内容的表现方式做保护处理,对上传内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(点击联系客服),我们立即给予删除!
4. 请根据预览情况,自愿下载本文。本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
5. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007及以上版本和PDF阅读器,压缩文件请下载最新的WinRAR软件解压。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Python 爬虫 程序设计 KC23
- 资源描述:
-
1、2.3.1 BeautifulSoup查找查找HTML元素元素2.3.1 BeautifulSoup查找查找HTML元素元素查找文档的元素是我们爬取网页信息的重要手段,BeautifulSoup提供了一系列的查找元素的方法,其中功能强大的find_all函数就是其中常用的一个方法。find_all函数的原型如下:find_all(self,name=None,attrs=,recursive=True,text=None,limit=None,*kwargs)self表明它是一个类成员函数;name是要查找的tag元素名称,默认是None,如果不提供,就是查找所有的元素;attrs是元素的属性
2、,它是一个字典,默认是空,如果提供就是查找有这个指定属性的元素;recursive指定查找是否在元素节点的子树下面全范围进行,默认是True;后面的text、limit、kwargs参数比较复杂,将在后面用到时介绍;find_all函数返回查找到的所有指定的元素的列表,每个元素是一个bs4.element.Tag对象。find_all函数是查找所有满足要求的元素节点,如果我们只查找一个元素节点,那么可以使用find函数,它的原型如下:find(self,name=None,attrs=,recursive=True,text=None,limit=None,*kwargs)使用方法与find_
3、all类似,不同的是它只返回第一个满足要求的节点,不是一个列表。例例2-3-1:查找文档中的:查找文档中的元素元素from bs4 import BeautifulSoupdoc=The Dormouses storyThe Dormouses storyOnce upon a time there were three little sisters;and their names wereElsie,Lacie andTillie;and they lived at the bottom of a well.soup=BeautifulSoup(doc,lxml)tag=soup.find(
4、title)print(type(tag),tag)程序结果:The Dormouses story由此可见查找到元素,元素类型是一个bs4.element.Tag对象。例例2-3-2:查找文档中的所有:查找文档中的所有元素元素from bs4 import BeautifulSoupdoc=The Dormouses storyThe Dormouses storyOnce upon a time there were three little sisters;and their names wereElsie,Lacie andTillie;and they lived at the bo
5、ttom of a well.soup=BeautifulSoup(doc,lxml)tags=soup.find_all(a)for tag in tags:print(tag)程序结果找到3个元素:ElsieLacieTillie例例2-3-3:查找文档中的第一个:查找文档中的第一个元素元素from bs4 import BeautifulSoupdoc=The Dormouses storyThe Dormouses storyOnce upon a time there were three little sisters;and their names wereElsie,Lacie
6、andTillie;and they lived at the bottom of a well.soup=BeautifulSoup(doc,lxml)tag=soup.find(a)print(tag)程序结果找到第一个元素:Elsie例例2-3-4:查找文档中:查找文档中class=title的的元素元素from bs4 import BeautifulSoupdoc=The Dormouses storyThe Dormouses storyOnce upon a time there were three little sisters;and their names wereElsi
7、e,Lacie andTillie;and they lived at the bottom of a well.soup=BeautifulSoup(doc,lxml)tag=soup.find(p,attrs=class:title)print(tag)程序结果找到class=title的元素The Dormouses story很显然如果使用:tag=soup.find(p)也能找到这个元素,因为它是文档的第一个元素。例例2-3-5:查找文档中:查找文档中class=sister的元素的元素from bs4 import BeautifulSoupdoc=The Dormouses st
8、oryThe Dormouses storyOnce upon a time there were three little sisters;and their names wereElsie,Lacie andTillie;and they lived at the bottom of a well.soup=BeautifulSoup(doc,lxml)tags=soup.find_all(name=None,attrs=class:sister)for tag in tags:print(tag)其中name=None表示无论是什么名字的元素,程序结果找到3个:a class=siste
展开阅读全文