beautifulsoup的总结

本文介绍如何使用BeautifulSoup库中的find_all方法,通过ID、href、class等属性进行精确的网页内容抓取。涵盖字符串、正则表达式等多种过滤器的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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')#返回特定idtag对象
#[<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 Soup4.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 的限制时,就停止搜索返回结果.

文档树中有3tag符合搜索条件,但结果只返回了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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值