html_doc="""
<html><head><title>TheDormouse'sstory</title></head>
<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>
"""
Id过滤器的用法
1.soup.find_all(id='link2')#返回特定id的tag对象
#[<a class="sister" href="http://example.com/lacie"id="link2">Lacie</a>]
2.
soup.find_all(id=True)#找到所有有id属性的tag,返回的是tag对象的列表
# [<a class="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">Tillie</a>]
Href过滤器的用法
soup.find_all(href=re.compile("elsie"))//通过href来过滤元素
# [<a class="sister"href="http://example.com/elsie"id="link1">Elsie</a>]
Src图片过滤器的用法
因为<img>tag 有个src属性,所以就可以用src来获取网页所有的图片啦
<imgsrc="http://image.ylyq.duoku.com/uploads/images/2016/1017/1476676420949247.jpg"width="150" height="200">
<imgsrc="http://image.ylyq.duoku.com/uploads/images/2016/0802/1470133017414468.png"width="150" height="200">
ele=soup.find_all(src=re.compile('http://image'))
也可以将上面的过滤器写在一起:
soup.find_all(href=re.compile("elsie"), id='link1')
#[<a class="sister" href="http://example.com/elsie"id="link1">three</a>]
Soup.find_all(href=re.compile('http://'),id='someid',src='…')
以通过 find_all() 方法的 attrs 参数定义一个字典参数来搜索包含特殊属性的tag:
data_soup.find_all(attrs={"data-foo": "value",'class':'someclass'})
#[<div data-foo="value">foo!</div>]
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
按照CSS类名搜索tag的功能非常实用,但标识CSS类名的关键字 class 在Python中是保留字,使用 class 做参数会导致语法错误.从Beautiful Soup的4.1.1版本开始,可以通过 class_ 参数搜索有指定CSS类名的tag:
soup.find_all("a", class_="sister")
#[<a class="sister" href="http://example.com/elsie"id="link1">Elsie</a>,
# <a class="sister"href="http://example.com/lacie"id="link2">Lacie</a>,
# <a class="sister"href="http://example.com/tillie"id="link3">Tillie</a>]
class_ 参数同样接受不同类型的 过滤器 ,字符串,正则表达式,方法或 True :
soup.find_all(class_=re.compile("itl"))
#[<p class="title"><b>The Dormouse'sstory</b></p>]
通过 text 参数可以搜搜文档中的字符串内容.与 name 参数的可选值一样, text 参数接受 字符串 , 正则表达式 , 列表, True . 看例子:
soup.find_all(text=re.compile("Dormouse"))
[u"The Dormouse's story", u"The Dormouse's story"]
虽然 text 参数用于搜索字符串,还可以与其它参数混合使用来过滤tag.BeautifulSoup会找到 .string 方法与 text 参数值相符的tag.下面代码用来搜索内容里面包含“Elsie”的<a>标签:
soup.find_all("a", text="Elsie")
#[<a href="http://example.com/elsie" class="sister"id="link1">Elsie</a>]
find_all() 方法返回全部的搜索结构,如果文档树很大那么搜索会很慢.如果我们不需要全部结果,可以使用 limit 参数限制返回结果的数量.效果与SQL中的limit关键字类似,当搜索到的结果数量达到 limit 的限制时,就停止搜索返回结果.
文档树中有3个tag符合搜索条件,但结果只返回了2个,因为我们限制了返回数量:
soup.find_all("a", limit=2)
#[<a class="sister" href="http://example.com/elsie"id="link1">Elsie</a>,
# <a class="sister"href="http://example.com/lacie"id="link2">Lacie</a>]
From <https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/#true>