版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、2.3BeautifulSoup查找HTML元素深圳信息职业技术学院Shenzhen Institute Of Information Technology教师:黄锐军目 录COMPANYBeautifulSoup查找HTML元素2.3.1BeautifulSoup获取元素的属性值2.3.2BeautifulSoup获取元素包含的文本值2.3.3BeautifulSoup高级查找2.3.4PART ONE BeautifulSoup查找HTML元素BeautifulSoup查找HTML元素查找文档的元素是我们爬取网页信息的重要手段,BeautifulSoup提供了一系列的查找元素的方法,其一个
2、方法。find_all函数的原型如下:能强大的find_all函数就是其中常用的find_all(self, name=None, attrs=, recursive=True, text=None, limit=None,*kwargs)self表明它是一个类成员函数;name是要查找的tag元素名称,默认是None,如果不提供,就是查找所有的元素;attrs是元素的属性,它是一个字典,默认是空,如果提供就是查找有这个指定属性的元素;recursive指定查找是否在元素节点的子树下面全范围迚行,默认是True;后面的text、limit、kwargs参数比较复杂,将在后面用到时介绍;find
3、_all函数返回查找到的所有指定的元素的列表,每个元素是一个bs4.element.Tag对象。find_all函数是查找所有满足要求的元素节点,如果我们只查找一个元素节点,那么可以使用find函数,它的原型如下: find(self, name=None, attrs=, recursive=True, text=None, limit=None, *kwargs)使用方法与find_all类似,不同的是它只返回第一个满足要求的节点,不是一个列表。例2-3-1:查找文档中的元素from bs4 import BeautifulSoup doc=The Dormouses storyThe D
4、ormouses 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(title)print(type(tag),tag)程序结果: The Dormouses story由此可见查找到元素,元素类型是一个bs4.element.Tag对象。例2-3-2:查找文档中的所有元素from bs4
5、 import BeautifulSoupdoc=The Dormouses storyThe Dormouses storyOnce upon a time there were three little sisters; and their nameswereElsie,Lacie andTillie;and they lived at the bottom of a well. soup=BeautifulSoup(doc,lxml) tags=soup.find_all(a)for tag in tags:print(tag)程序结果找到3个元素:ElsieLacieTillie例2-
6、3-3:查找文档中的第一个元素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(a)print(tag)程序结果找到第一个元素:Elsie例2-3-4:查找文
7、档中class=title的元素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(p,attrs=class:title)print(tag)程序结果找到cl
8、ass=title的元素The Dormouses story很显然如果使用:tag=soup.find(p)也能找到这个元素,因为它是文档的第一个元素。例2-3-5:查找文档中class=sister的元素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
9、 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=sister href=/elsie id=link1ElsieLacieTillie对于这个文档,很显然语句: tags=soup.find_all(a)或 者 : tags=soup.find_all(a,attrs=class:sister)效果一样。PA
10、RT TWO BeautifulSoup获取元素的属性值BeautifulSoup获取元素的属性值如果一个元素已经找到,例如找到元素,那么怎么样获取它的属性值呢?BeautifulSoup使用: tagattrName来获取tag元素的名称为attrName的属性值,其中tag是一个bs4.element.Tag对象。例2-3-6:查找文档中所有超级链接地址from bs4 import BeautifulSoupdoc=The Dormouses storyThe Dormouses storyOnce upon a time there were three little sisters;
11、 and their names wereElsie,Lacie andTillie; and they lived at the bottom of a well. soup=BeautifulSoup(doc,lxml) tags=soup.find_all(a)for tag in tags:print(taghref)程 序 结 果 : /elsie /lacie/tilliePART Three BeautifulSoup获取元素包含的文本值BeautifulSoup获取元素包含的文
12、本值如果一个元素已经找到,例如找到元素,那么怎么样获取它包含的文本值呢?BeautifulSoup使用: tag.text来获取tag元素包含的文本值,其中tag是一个bs4.element.Tag对象。例2-3-7:查找文档中所有超级链接包含的文本值from bs4 import BeautifulSoup doc=The Dormouses storyThe Dormouses storyOnce upon a time there were three little sisters; and their names wereElsie,Lacie andTillie; and they
13、lived at the bottom of a well. soup=BeautifulSoup(doc,lxml) tags=soup.find_all(a)for tag in tags:print(tag.text)程序结果: Elsie LacieTillie例2-3-8:查找文档中所有超级链接包含的文本值from bs4 import BeautifulSoup doc=The Dormouses storyThe Dormouses storyOnce upon a time there were three little sisters; and their names wer
14、eElsie,Lacie andTillie; and they lived at the bottom of a well.soup=BeautifulSoup(doc,lxml) tags=soup.find(p)for tag in tags: print(tag.text)程序结果:The Dormouses storyOnce upon a time there were three little sisters; and their names were Elsie,Lacie and Tillie;and they lived at the bottom of a well.其中
15、第二个包含的值就是节点子树下面所有文本节点的组合值。PART Four BeautifulSoup高级查找BeautifulSoup高级查找一般find或者find_all都能满足我们的需要,如果还不能那么可以设计一个查找函数来迚行查找。例2-3-9:我们查找文档中的href=/lacie的节点元素from bs4 import BeautifulSoupdoc=The Dormouses storyElsieLacieTilliedef myFilter(tag): print()return (=a and tag.has_
16、attr(href) andtaghref=/lacie)soup=BeautifulSoup(doc,lxml) tag=soup.find_all(myFilter)print(tag)程序结果:html head title body aaaLacie说明:在程序中我们定义了一个筛选函数myFilter(tag),它的参数是tag对象, 在调用soup.find_all(myFilter)时程序会把每个tag元素传递给myFilter函数,由该函数决定这个tag的取舍,如果myFilter返回True就保留这个tag 到结果集中,不然就丢掉这个tag。因
17、此程序执行时可以看到html,body,head,title,body,a,a,a等一个个tag经过myFilter的筛选,只有节点Lacie满足要求,因此结果为:Lacie其中:是tag的名称;tag.has_attr(attName)判断tag是否有attName属性;tagattName是tag的attName属性值;例2-3-10:通过函数查找可以查找到一些复杂的节点元素,查找文本值以cie结尾所有节点from bs4 import BeautifulSoup doc=The Dormouses storyElsieLacieTillieTilciedef endsWith(s,t): if len(s)=len(t):return slen(s)-len(t):=treturn Falsedef myFilte
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 直肠癌新辅助治疗后等待观察策略研究2026
- 妇产科护理中的患者权利保护
- 专题24 第三产业与经济发展说课稿2025学年高中地理中图版上海第二册-中图版上海2007
- 尺桡骨骨折手术心理护理方法
- 初中心理教育教案2025年生涯规划设计
- 2026年纹绣说课稿语文模板
- 初中生语文戏剧创意说课稿2025
- 元、明、清诗词说课稿2025学年统编版小初衔接-统编版(小初衔接)
- 第三节 设计一个声控灯-模拟IO口的使用说课稿2025学年初中信息技术西交大版2014九年级下册-西交大版2014
- 未成年人救助保护中心建设项目可行性研究报告
- 2026年教科版(新教材)小学科学三年级下册期末学情测试卷及答案
- 2026年国际汉语教师证书考试面试常考试题与答案
- 2026安徽省滁州市皖东公证处招聘司法辅助劳务派遣人员3人笔试备考试题及答案解析
- 12.2跨学科实践:制作简易杆秤课时练习(含答案)八年级下册物理人教版 (2024)
- 2026年上海市黄浦区中考数学二模试卷(含解析)
- 水库建设项目建议书
- DB31∕T 1676-2026 地震预警信息发布要求
- 川上未映子《乳与卵》中的女性身体叙事研究
- 《2023版CSCO小细胞肺癌诊疗指南》
- 2026年院感培训知识测试题及答案
- 四不伤害安全培训课件
评论
0/150
提交评论