【文档说明】Python爬虫程序设计KC23.pptx,共(22)页,76.896 KB,由小橙橙上传
转载请保留链接:https://www.ichengzhen.cn/view-2412.html
以下为本文档部分文字说明:
2.3.1BeautifulSoup查找HTML元素2.3.1BeautifulSoup查找HTML元素查找文档的元素是我们爬取网页信息的重要手段,BeautifulSoup提供了一系列的查找元素的方法,其中功能
强大的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_all函数返回查找到的所有指定的元素的列表,每个元素是一个bs4.element.Tag对象。find_all函数是查找所有满足要求的元素节点,如果我
们只查找一个元素节点,那么可以使用find函数,它的原型如下:find(self,name=None,attrs={},recursive=True,text=None,limit=None,**kwargs)使用方法与find_all类似,不同的是它只返回第一个
满足要求的节点,不是一个列表。例2-3-1:查找文档中的<title>元素frombs4importBeautifulSoupdoc='''<html><head><title>TheDormouse'sstory</
title></head><body><pclass="title"><b>TheDormouse'sstory</b></p><pclass="story">Onceuponatimetherewerethreelittlesisters;andtheirnameswere
<ahref="http://example.com/elsie"class="sister"id="link1">Elsie</a>,<ahref="http://example.com/lacie"class="sister"id="link2">Lacie</a>a
nd<ahref="http://example.com/tillie"class="sister"id="link3">Tillie</a>;andtheylivedatthebottomofawell.</p><pclass="story">...</p
></body></html>'''soup=BeautifulSoup(doc,"lxml")tag=soup.find("title")print(type(tag),tag)程序结果:<class'bs4.elem
ent.Tag'><title>TheDormouse'sstory</title>由此可见查找到<title>元素,元素类型是一个bs4.element.Tag对象。例2-3-2:查找文档中的所有<a>元素frombs4importBeautifulS
oupdoc='''<html><head><title>TheDormouse'sstory</title></head><body><pclass="title"><b>TheDormouse'sstory</b></p><pclass
="story">Onceuponatimetherewerethreelittlesisters;andtheirnameswere<ahref="http://example.com/elsie"class="sister"id="link1">Elsie</
a>,<ahref="http://example.com/lacie"class="sister"id="link2">Lacie</a>and<ahref="http://example.com/tillie"class="sister"id="link3">Tillie<
/a>;andtheylivedatthebottomofawell.</p><pclass="story">...</p></body></html>'''soup=BeautifulSoup(doc,"lxml")tags=soup.find_all("a")fortagintag
s:print(tag)程序结果找到3个<a>元素:<aclass="sister"href="http://example.com/elsie"id="link1">Elsie</a><aclass="sister"href
="http://example.com/lacie"id="link2">Lacie</a><aclass="sister"href="http://example.com/tillie"id="link3">T
illie</a>例2-3-3:查找文档中的第一个<a>元素frombs4importBeautifulSoupdoc='''<html><head><title>TheDormouse'sstory</title></head><body><pclass="title">
<b>TheDormouse'sstory</b></p><pclass="story">Onceuponatimetherewerethreelittlesisters;andtheirnameswere<ahref="http://example.com/elsie"class
="sister"id="link1">Elsie</a>,<ahref="http://example.com/lacie"class="sister"id="link2">Lacie</a>and<ahref="htt
p://example.com/tillie"class="sister"id="link3">Tillie</a>;andtheylivedatthebottomofawell.</p><pclass="story">.
..</p></body></html>'''soup=BeautifulSoup(doc,"lxml")tag=soup.find("a")print(tag)程序结果找到第一个<a>元素:<aclass="sister"h
ref="http://example.com/elsie"id="link1">Elsie</a>例2-3-4:查找文档中class="title"的<p>元素frombs4importBeautifulSoup
doc='''<html><head><title>TheDormouse'sstory</title></head><body><pclass="title"><b>TheDormouse'sstory</b></p><pclass
="story">Onceuponatimetherewerethreelittlesisters;andtheirnameswere<ahref="http://example.com/elsie"class="sister"id="link1">Elsie</a>,<ahr
ef="http://example.com/lacie"class="sister"id="link2">Lacie</a>and<ahref="http://example.com/tillie"class="sister"id="link3">Tillie</a>;andtheylived
atthebottomofawell.</p><pclass="story">...</p></body></html>'''soup=BeautifulSoup(doc,"lxml")tag=soup.find("p",
attrs={"class":"title"})print(tag)程序结果找到class="title"的<p>元素<pclass="title"><b>TheDormouse'sstory</b></p>很显然如果使用:tag
=soup.find("p")也能找到这个元素,因为它是文档的第一个<p>元素。例2-3-5:查找文档中class="sister"的元素frombs4importBeautifulSoupdoc='''<html><head><title>TheDormo
use'sstory</title></head><body><pclass="title"><b>TheDormouse'sstory</b></p><pclass="story">Onceuponatimetherewerethreelittlesisters;andthe
irnameswere<ahref="http://example.com/elsie"class="sister"id="link1">Elsie</a>,<ahref="http://example.com/lacie"class="sister"id="link2">Laci
e</a>and<ahref="http://example.com/tillie"class="sister"id="link3">Tillie</a>;andtheylivedatthebottomofa
well.</p><pclass="story">...</p></body></html>'''soup=BeautifulSoup(doc,"lxml")tags=soup.find_all(name=None,attrs={"class":"sister"})fortagint
ags:print(tag)其中name=None表示无论是什么名字的元素,程序结果找到3个:aclass="sister"href="http://example.com/elsie"id="link1">Elsie</
a><aclass="sister"href="http://example.com/lacie"id="link2">Lacie</a><aclass="sister"href="http://example.com/tillie"id="link3">T
illie</a>对于这个文档,很显然语句:tags=soup.find_all("a")或者:tags=soup.find_all("a",attrs={"class":"sister"})效果一样。2.3.2BeautifulSoup获取元素的属性值2
.3.2BeautifulSoup获取元素的属性值如果一个元素已经找到,例如找到<a>元素,那么怎么样获取它的属性值呢?BeautifulSoup使用:tag[attrName]来获取tag元素的名称为attrNam
e的属性值,其中tag是一个bs4.element.Tag对象。例2-3-6:查找文档中所有超级链接地址frombs4importBeautifulSoupdoc='''<html><head><title>TheDormouse'sstory</title></
head><body><pclass="title"><b>TheDormouse'sstory</b></p><pclass="story">Onceuponatimetherewerethreelittlesisters;andtheirnameswer
e<ahref="http://example.com/elsie"class="sister"id="link1">Elsie</a>,<ahref="http://example.com/lacie"class="sister"id="link2">Laci
e</a>and<ahref="http://example.com/tillie"class="sister"id="link3">Tillie</a>;andtheylivedatthebottomofawell.</p><pclass="story">...</p><
/body></html>'''soup=BeautifulSoup(doc,"lxml")tags=soup.find_all("a")fortagintags:print(tag["href"])程序结果:http://
example.com/elsiehttp://example.com/laciehttp://example.com/tillie2.3.3BeautifulSoup获取元素包含的文本值2.3.3Beaut
ifulSoup获取元素包含的文本值如果一个元素已经找到,例如找到<a>元素,那么怎么样获取它包含的文本值呢?BeautifulSoup使用:tag.text来获取tag元素包含的文本值,其中tag是一个bs4.element.Tag
对象。例2-3-7:查找文档中所有<a>超级链接包含的文本值frombs4importBeautifulSoupdoc='''<html><head><title>TheDormouse'sstory</title></head><body><pclass="titl
e"><b>TheDormouse'sstory</b></p><pclass="story">Onceuponatimetherewerethreelittlesisters;andtheirnameswere<ahref=
"http://example.com/elsie"class="sister"id="link1">Elsie</a>,<ahref="http://example.com/lacie"class="sister"id="link2">Lacie</a
>and<ahref="http://example.com/tillie"class="sister"id="link3">Tillie</a>;andtheylivedatthebottomofawell.
</p><pclass="story">...</p></body></html>'''soup=BeautifulSoup(doc,"lxml")tags=soup.find_all("a")fortagintags:print(tag.te
xt)程序结果:ElsieLacieTillie例2-3-8:查找文档中所有<p>超级链接包含的文本值frombs4importBeautifulSoupdoc='''<html><head><title>TheDormouse'sstory</t
itle></head><body><pclass="title"><b>TheDormouse'sstory</b></p><pclass="story">Onceuponatimetherewerethreelittlesisters;andtheirnam
eswere<ahref="http://example.com/elsie"class="sister"id="link1">Elsie</a>,<ahref="http://example.com/lacie"class="s
ister"id="link2">Lacie</a>and<ahref="http://example.com/tillie"class="sister"id="link3">Tillie</a>;andtheylivedatthe
bottomofawell.</p><pclass="story">...</p></body></html>'''soup=BeautifulSoup(doc,"lxml")tags=soup.find("p")fortagintags:print(tag.text)程序结果:The
Dormouse'sstoryOnceuponatimetherewerethreelittlesisters;andtheirnameswereElsie,LacieandTillie;andtheylivedatthebotto
mofawell....其中第二个<p>包含的值就是<p>节点子树下面所有文本节点的组合值。2.3.4BeautifulSoup高级查找一般find或者find_all都能满足我们的需要,如果还不能那么可以设计一个查找函数来进行查找。例2-3-9:我们查找文档中的href="http://
example.com/lacie"的节点元素<a>frombs4importBeautifulSoupdoc='''<html><head><title>TheDormouse'sstory</title></head><bod
y><ahref="http://example.com/elsie">Elsie</a><ahref="http://example.com/lacie">Lacie</a><ahref="http://example.com/tillie">Tillie</a></body><
/html>'''defmyFilter(tag):print(tag.name)return(tag.name=="a"andtag.has_attr("href")andtag["href"]=="http://example.
com/lacie")soup=BeautifulSoup(doc,"lxml")tag=soup.find_all(myFilter)print(tag)程序结果:htmlheadtitlebodyaaa[<ahr
ef="http://example.com/lacie">Lacie</a>]说明:在程序中我们定义了一个筛选函数myFilter(tag),它的参数是tag对象,在调用soup.find_all(myFilter)时程序会把每个tag元素传递给myFilter函数,由该函数决定这个t
ag的取舍,如果myFilter返回True就保留这个tag到结果集中,不然就丢掉这个tag。因此程序执行时可以看到html,body,head,title,body,a,a,a等一个个tag经过my
Filter的筛选,只有节点<ahref="http://example.com/lacie">Lacie</a>满足要求,因此结果为:[<ahref="http://example.com/lacie">Lacie</a>]其
中:tag.name是tag的名称;tag.has_attr(attName)判断tag是否有attName属性;tag[attName]是tag的attName属性值;例2-3-10:通过函数查找可以查找到一些复杂
的节点元素,查找文本值以"cie"结尾所有<a>节点frombs4importBeautifulSoupdoc='''<html><head><title>TheDormouse'sstory</title
></head><body><ahref="http://example.com/elsie">Elsie</a><ahref="http://example.com/lacie">Lacie</a><ahref="http://example.com/tillie"
>Tillie</a><ahref="http://example.com/tilcie">Tilcie</a></body></html>'''defendsWith(s,t):iflen(s)>=len(t):returns[len(s)-len(t):]==t
returnFalsedefmyFilter(tag):return(tag.name=="a"andendsWith(tag.text,"cie"))soup=BeautifulSoup(doc,"lxml")ta
gs=soup.find_all(myFilter)fortagintags:print(tag)程序结果:<ahref="http://example.com/lacie">Lacie</a><ahref=
"http://example.com/tilcie">Tilcie</a>程序中定义了一个endsWIth(s,t)函数判断s字符串是否以字符串t结尾,是就返回True,不然返回False,在myFilter
中调用这个函数判断tag.text是否以"cie"结尾,最后找出所有文本值以"cie"结尾的<a>节点。