Beautiful Soup的用法(四):find和find_all的使用

使用Beautiful Soup 对网页进行解析,需要根据网页的结构找到自己需要的关键信息,在找分析网页结构和找出关键信息就经常用到的两个函数为findfind_all

前面我们已经知道,findfind_all是有区别的,主要的区别是,find 的结果是返回一个bs4.element.Tag的对象,而find_all 返回的是一个bs4.element.ResultSet的对象,bs4.element.ResultSet中是一个 bs4.element.Tag的链表。

findfind_all 的用法基本上没有什么区别,介绍一个find的使用就能够推广到find_all的用法。

find的原型为:

find( name , attrs , recursive , text , **kwargs )

name 也就是html中的标签,如下使用,还是用到之前的数据:

from bs4 import BeautifulSoup  
  
html = """  
<html><head><title>The Dormouse's story</title></head>  
<body>  
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>  
<p class="story">Once upon a time there were three little sisters; and their names were  
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and  
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;  
and they lived at the bottom of a well.</p>  
</body>  
</html>  
"""  
  
soup = BeautifulSoup(html, "lxml")  
body_tag = soup.find(name='body')  
print body_tag

输出的结果为:

<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
</body>

找到了标签名为body 的第一段html
如果采用的是

soup.find(name='p')  

会是怎样的结果呢? 因为html代码中有多个的p标签?find的时候,如果有多个标签的话会返回最先找到的第一个标签。也就是

<p class="title" name="dromouse"><b>The Dormouse's story</b></p>

findfind_all 同样支持 正则表达式 查找,如找出name中含有 a或者是ptag

import re
tags = soup.find_all(name=re.compile('a|p'))  
for tag in tags:  
    print tag

输出的结果会是什么呢?

<head><title>The Dormouse's story</title></head>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

在使用name 还是无法容易的找出我们需要的tag 时,这时候可以结合上attrs 的查找方式。

attrs 需要我们给出的值为 字典形式的数据,如下找出class="title"tag

tags = soup.find_all(name=re.compile('a|p'),attrs={'class':'title'})  
for tag in tags:  
    print tag

输出的结果为:

<p class="title" name="dromouse"><b>The Dormouse's story</b></p>

使用 text 进行查找:

tags = soup.find_all(name=re.compile('a|p'),text='Lacie')  
for tag in tags:  
    print tag

输出的结果如下:

<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>

同样的可以使用[]数组进行查找:

tags = soup.find_all(name=re.compile('a|p'),text=['Lacie','Tillie'])  
for tag in tags:  
    print tag

值得注意的是,所有的查找方式都支持正则表达式数组查找

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

go2coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值