<居然讲爬虫>5-数据解析之BeautifulSoup4

BeautifulSoup是Python的一个库,用于从网页抓取数据。它提供了简单的Python接口来导航和解析HTML文档。虽然性能略低于lxml,但因其易用性而受欢迎。基本使用包括创建BeautifulSoup对象,如soup=BeautifulSoup(html_doc,lxml),以及使用find()和find_all()搜索文档树。此外,还支持CSS选择器进行更精确的元素定位。

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

前面我给大家介绍了正则和xpath,接下来我们在来看一个更简单的数据解析的模块Beautiful Soup

Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据,官方解释如下:

  • Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序。
  • Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。你不需要考虑编码方式,除非文档没有指定一个编码方式,这时,Beautiful Soup就不能自动识别编码方式了。然后,你仅仅需要说明一下原始编码方式就可以了。
  • Beautiful Soup已成为和lxml一样出色的python解释器,为用户灵活地提供不同的解析策略或强劲的速度。

src=http___img.soogif.com_4W3kUgxh6OY8k7fWp8VcYsZ3b0EyhZRf.JPEG&refer=http___img.soogif.webp

一般对于这种官方的解释,我们大概看一眼就行。因为官方的解释一般都不说人话,对于零基础的小伙伴来说理解起来可能会很困难。

和lxml一样,BeautifulSoup也是一个HTML/XML的解析器,主要功能也是如何解析和提取HTML/XML数据。

lxml只会局部遍历,而BeautifulSoup是基于HTML DOM的,会载入整个文档,解析整个DOM树,因此时间和内存开销都会很大,所以性能要低于lxml BeautifulSoup用来解析HTML比较简单,支持CSS选择器,Python标准库中的HTML解析器,也支持lxml的XML解析器。

BeautifulSoup安装 pip install bs4

解析工具对比

抓取工具速度使用难度
正则最快困难
lxml简单
BeautifulSoup最简单

BeautifulSoup的基本使用

# 导入模块
from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><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/elsie" class="sister" id="link1">Elsie</a>,
<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>

<p class="story">...</p>
"""

# 创建Beautiful Soup对象
soup = BeautifulSoup(html_doc)

如果不传任何的解析器,会报错

UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently

解析器我们可以传

clipboard.png

我们一般会用lxmllxml需要安装pip install lxml

soup = BeautifulSoup(html_doc, lxml)

接下来我们就可以用BeautifulSoup的语法了

搜索文档树

Beautiful Soup定义了很多搜索方法,这里着重介绍2个: find()find_all()

from bs4 import BeautifulSoup

html_doc = """
<table class="tablelist" cellspacing="0" cellpadding="0">
    <tbody>
        <tr class="h">
            <td class="1" width="374">职位名称</td>
            <td>职位类别</td>
            <td>人数</td>
            <td>地点</td>
            <td>发布时间</td>
        </tr>
        <tr class="even">
            <td class="l"><a href="https://www.baidu.com">Python开发</a></td>
            <td class="l">技术类</td>
            <td class="l">1</td>
            <td class="l">深圳</td>
            <td class="l">2018-11-25</td>
        </tr>
        <tr>
            <td><a href="https://www.qq.com">数据分析</a></td>
            <td>技术类</td>
            <td>2</td>
            <td>深圳</td>
            <td>2018-11-24</td>
        </tr>
        <tr>
            <td><a href="https://www.juran.com">机器学习</a></td>
            <td>技术类</td>
            <td>2</td>
            <td>深圳</td>
            <td>2018-11-24</td>
        </tr>
        <tr>
            <td><a href="https://www.python.com">爬虫工程师</a></td>
            <td>技术类</td>
            <td>2</td>
            <td>深圳</td>
            <td>2018-11-24</td>
        </tr>
        <tr>
            <td><a href="https://www.lg.com" id="test" class="test">NLP自然语言处理</a></td>
            <td>技术类</td>
            <td>2</td>
            <td>深圳</td>
            <td>2018-11-24</td>
        </tr>
    </tbody>
</table>

如果我想获取所有的tr标签

soup = BeautifulSoup(html_doc, 'lxml')

trs = soup.find_all('tr')

print(trs)    # 列表,用循环取值

获取第2个tr标签 limit取前2个标签,然后用下标取第二个

tr = soup.find_all('tr', limit=2)[1]
print(tr)

获取所有class等于even的tr标签

trs = soup.find_all('tr', {"class": "even"})

将所有id等于test,class也等于test的a标签提取出来

alist = soup.find_all('a', {"id": "test", "class": "test"})

获取所有的a标签的href属性

hrefs = soup.find_all('a')
for a in hrefs:
    # 1.通过下表操作的方式
    href = a['href']

获取所有的职位信息(纯文本)

# 过滤掉第一个
trs = soup.find_all('tr')[1:]
for tr in trs:
    tds = tr.find_all("td")
    title = tds[0]
    # 获取到字符串 单独打印title是a标签
    print(title.string)

findfind_all的区别

  • find找到满足条件的第一个标签,只会返回一个元素
  • find_all找到满足所有条件的标签,返回的是列表

CSS常用选择器介绍

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        # 选择p标签
        p{
            background-color:red;
        }
        # 根据class选择p标签
        .line1{
            background-color:pink;
        }
        #line3{
            background-color:blue;
        }
        # div下面的p标签
        .box p{
            background-color:pink;
        }

        # 选择div class下面直接的p标签
        .box > p {
            background-color:pink;
        }
        # 选择name=username的input标签
        input[name="username"]{
            background-color:pink;
        }

    </style>
</head>
<body>
   <div class="box">
       <div>
           <p>这是第零行</p>
       </div>
       <p class="line1">这是第一行</p>
       <p class="line1">这是第二行</p>
       <p id="line3">这是第三行</p>
   </div>
    <p>这是第四行</p>

   <input type="text" name="username">
   <input type="text" name="password">
</body>
</html>
  • 通过标签名查找
print(soup.select('a'))
  • 通过类名查找
print(soup.select('.sister'))   查找class=sister的标签
  • 通过id查找
print(soup.select("#link1"))
  • 组合查找

    • 组合查找即和写 class 文件时,标签名与类名、id名进行的组合原理是一样的,例如查找 p 标签中,id 等于 link1的内容,二者需要用空格分开:
print(soup.select("p #link1"))

直接子标签查找,则使用 > 分隔:

print(soup.select("head > title"))
  • 通过属性查找
print(soup.select('a[href="http://example.com/elsie"]'))
  • 获取内容

    • 以上的 select 方法返回的结果都是列表形式,可以遍历形式输出,然后用 get_text() 方法来获取它的内容。

遍历文档树

contents和children
tr = soup.find('tr')
# 返回所有子节点的列表  获取第一个tr下面的所有td
print(tr.contents)
# 返回所有子节点的迭代器 获取第一个tr下面的所有td
print(list(tr.children))

父节点

通过 .parent 属性来获取某个元素的父节点.
tdf = soup.find('td').parent
print(tdf)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

「已注销」

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

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

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

打赏作者

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

抵扣说明:

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

余额充值