跟着崔庆才学爬虫2:Beautiful Soup的使用(1)

简介

简单来说,Beautiful Soup时Python的一个HTML或XML的解析库,我们可以用它更方便的从页面中提取数据,其官方解释如下:

Beautiful Soup 提供了一些简单的、python式的函数来处理导航,搜索,修改分析树等功能,它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以无须很多代码就可以写出一个完整的应用程序。Beautiful Soup自动将输入文档转换成Unicode编码,将输出文档转换为utf-8编码,你不需要考虑编码方式,除非文档没有指定具体的编码方式,这时你仅仅需要说明一下原始编码方式就可以了。

总而言之,利用Beautiful Soup可以省去很多繁琐的提取工作,提高解析网络效率。

解析去

实际上,Beautiful Soup在解析时时依赖解析器的,它除了支持Python标准库中的HTML解析器,还支持一些第三方解析器(例如lxml)下面图表中列出了 Beautiful Soup支持的解析器

准备工作

在开始之前,请确保已经正确安装好Beautiful Soup和lxml这两个库,lxml在上节使用xpath安装方法已经说过,这里直接展示Beautiful Soup的安装方法

pip install beautifulsoup4

 安装完成后使用pip list指令查看是否安装完成

 基本使用

下面首先通过实例看看Beautiful Soup的基本用法

from bs4 import BeautifulSoup

html = '''<html><head><title>This Dormouse's story</title></head>
<body>
    <p class="title" name="dormouse"><b>This 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>
'''
soup = BeautifulSoup(html,'lxml')
print(soup.prettify())
print(soup.title.string)

运行结果如下:

<html>
 <head>
  <title>
   This Dormouse's story
  </title>
 </head>
 <body>
  <p class="title" name="dormouse">
   <b>
    This 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/elsie" id="link1">
    <!--Elsie-->
   </a>
   <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>
  <p class="story">
   ...
  </p>
 </body>
</html>

This Dormouse's story

 这里首先声明一个变量html,这是一个HTML字符串,但是需要注意的是,它并不是一个完整的字符串,因为body和html节点都没有闭合,接着,我们将它当作第一个参数传入给Beautiful Soup对象,该对象第二个参数为解析器的类型,这里使用的是lxml,此时就完成了Beautiful Soup对象的初始化,然后将这个对象赋值给soup变量。

之后就调用soup的各个方法和属性解析这串HTML代码

首先调用prettify方法。这个方法可以把要解析的字符串以标准的缩进格式输出,这里需要注意的是,输出结果包含body和html节点,也就是说对于不标准的HTML字符串Beautiful Soup可以自动更正格式。这一步并不是prettify方法完成的,而是在Beautiful Soup初始化的时候就完成了。

然后调用soup.title.string,这里实际上是输出HTML中title节点的文本内容。所以通过soup.title选出HTML中的title节点,再调用string属性就可以得到title节点里面的文本了。

节点选择器

直接调用节点的名称即可选择节点,然后调用string属性就可以得到节点内的文本了。这种选择方式速度非常快,当单个节点结构层次非常清晰时,可以选用这种方式来解析。

示例如下:

from bs4 import BeautifulSoup

html = '''<html><head><title>This Dormouse's story</title></head>
<body>
    <p class="title" name="dormouse"><b>This 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>
'''
soup = BeautifulSoup(html,'lxml')
print(soup.title)
print(type(soup.title))
print(soup.title.string)
print(soup.head)
print(soup.p)

运行结果:<title>This Dormouse's story</title>
<class 'bs4.element.Tag'>
This Dormouse's story
<head><title>This Dormouse's story</title></head>
<p class="title" name="dormouse"><b>This Dormouse's story</b></p>

这里依然使用刚才的HTML代码。首先打印出title节点的选择结果,输出结果正是title节点以及里面的文字内容,接下来输出title节点的类型,是bs4.element.Tag,这是beautiful soup中一个重要的数据结构,经过选择器选择的结果都是这种Tag属性,Tag具有一些属性,例如string属性,调用该属性可以得到节点的文本内容,所以类型的输出结果正式节点的文本内容。

输出文本内容后,又尝试选择了head节点,结果也是节点加其内部的所有内容,最后,选择了p节点,不过这次情况比较特殊,因为结果是第一个p节点的内容,后面的几个p节点并没有选取到。也就是,当有多个节点时,这种选择方式只会选择到第一个匹配的节点,后面的其他节点都会忽略。

提取信息

上面演示了通过利用string属性获取文本的值,那么如何获取节点名称?如何获取节点属性的值呢?下面我们就统一梳理一下信息的提取方式

获取名称

利用name属性我们就可以获取节点的名称,以案例为例,先选取title节点,在调用name属性就可以得到节点名称:

print(soup.title.name)

输出结果:title

获取属性

一个节点可能有多个属性,例如id和class等,选择这个节点元素后,可以调用attrs获取其所有属性:

print(soup.p.attrs)
print(soup.p.attrs['name'])

运行结果:{'class': ['title'], 'name': 'dormouse'}
dormouse

可以看到,调用attrs属性返回结果是字典形式,包括所选择节点的所有属性和属性值,因此,要获取name属性,相当于从字典获取某个键值。只需要用中括号加属性名就可以了。例如通过attrs['name']获取name属性。

其实这种方法还是有些繁琐的,还有一种更为简单的获取属性值的方法,不用写attrs,直接再节点元素后面加中括号,然后传入属性名就可以了。样例如下:

print(soup.p['name'])
print(soup.p['class'])

运行结果:dormouse
['title']

获取内容

这点在之前说道过,可以用string属性获取节点元素包含的文本内容,例如用如下实例获取第一个p节点的文本:

print(soup.p.string)

 运行如下:This Dormouse's story

再次注意一下,这里选择的p节点是第一个节点,获取的文本也是第一个p节点的文本

嵌套选择

上面的例子我们知道所有返回结果都是bs4.element.Tag类型,Tag类型的对象同样可以继续调用节点进行下一步的选择。例如,我们获取了head节点,就可以继续调用head选取其内部的head节点:

from bs4 import BeautifulSoup
html='''
<html><head><title>The Dormouse's story</title></head></body>
'''
soup=BeautifulSoup(html,'lxml')
print(soup.head.title)
print(type(soup.head.title))
print(soup.head.title.string)

运行如下:<title>The Dormouse's story</title>
<class 'bs4.element.Tag'>
The Dormouse's story

运行结果的第一行是调用head之后再调用title,而选择的title节点。第二行打印出了它的类型,可以看到仍然是bs4.element.Tag类型,也就是说,我们可以再Tag类型的基础上再次选择,得到的结果依然是Tag类型,既然每次返回的结果都相同,那么就可以做嵌套选择。

最后一行结果输出了title节点的string属性,也就是节点的文本内容。

关联选择

在做选择的过程中,有时不能一步就选到想要的节点,需要先选中某一个节点,再以它为基准选择子节点,父节点,兄弟节点,下面就详细介绍一下如何选择这些节点。

子节点和子孙节点

选取节点之后,如果想要获取它的直接子节点,可以调用contents属性,实例如下:

from bs4 import BeautifulSoup

html = '''<html><head><title>This Dormouse's story</title></head>
<body>
  
    <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>
'''
soup = BeautifulSoup(html,'lxml')
print(soup.p.contents)

运行如下:

(.venv) D:\跟着崔庆才学爬虫>d:/跟着崔庆才学爬虫/.venv/Scripts/python.exe d:/跟着崔庆才学爬虫/bs4的使用/4_关联选择.py
['Once upon a time there were three little sisters;and their names were\n        ', <a class="sister" href="http://example.com/elsie" id="link1"><!--Elsie--></a>, '\n', <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, 'and\n        ', <a class="sister" href="http://example.com/tillie" id="link3">Tillie </a>, ';\n        and they lived at the bottom of a well .']

 可以看到,返回结果是列表形式。p节点里即包含文本,又包含节点,这些内容会以列表形式统一返回。

需要注意的是,列表中的每个元素都是p元素的直接子节点,像第一个a节点里面包含的span节点,就相当于孙子节点,但是返回结果并没有把span节点单独选出来,所以说,contents属性得到的结果是直接子节点组成的列表。

同样,我们可以调用children属性获得相应的结果:

print(soup.p.children)
for i ,child in enumerate(soup.p.children):
    print(i,child)

 运行如下:

0 Once upon a time there were three little sisters;and their names were

1 <a class="sister" href="http://example.com/elsie" id="link1"><!--Elsie--></a>
2

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

5 <a class="sister" href="http://example.com/tillie" id="link3">Tillie </a>
6 ;
        and they lived at the bottom of a well .

还是同样的HTML文本,这里调用children属性来选择,返回类型是生成器类型,然后我们用for循环输出了相应的内容。

如果想要得到所有的子孙节点,则可以调用descendants属性

print(soup.p.descendants)
for i ,child in enumerate(soup.p.descendants):
    print(i,child)

运行结果:

<generator object Tag.descendants at 0x000001CB4810E5E0>
0 Once upon a time there were three little sisters;and their names were

1 <a class="sister" href="http://example.com/elsie" id="link1"><!--Elsie--></a>
2 Elsie
3

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

7 <a class="sister" href="http://example.com/tillie" id="link3">Tillie </a>
8 Tillie
9 ;
        and they lived at the bottom of a well .

 你会发现,返回结果还是生成器,但是这里返回结果比之前包含了更多的内容,因为descendants会递归查询所有子节点,得到所有的子孙节点。

父节点和祖先节点

如果获取某个节点元素的父节点,可以调用parent属性:

from bs4 import BeautifulSoup

html = '''<html><head><title>This Dormouse's story</title></head>
<body>
  
    <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">
        <span>Elsie</span>
        </a>
        </p>
        <p class="story">...</p>
'''


soup = BeautifulSoup(html,'lxml')
print(soup.a.parent)

 运行结果如下:

<p class="story">Once upon a time there were three little sisters;and their names were
        <a class="sister" href="http://example.com/elsie" id="link1">
<span>Elsie</span>
</a>
</p>

这里我们选择的是a节点的父元素,很明显,a节点的父节点是p节点,所以输出结果就是p节点及其相关内容。

需要注意,这里输出的仅仅是a节点的直接父元素,而没有再向外寻找父节点的祖先节点,如果想获取所有祖先节点,可以调用parents属性:

from bs4 import BeautifulSoup

html = '''<html><head><title>This Dormouse's story</title></head>
<body>
  
    <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">
        <span>Elsie</span>
        </a>
        </p>
        <p class="story">...</p>
'''


soup = BeautifulSoup(html,'lxml')
print(soup.a.parents)
print(type(soup.a.parents))
print(list(enumerate(soup.a.parents)))

运行结果如下:<generator object PageElement.parents at 0x000001FC5DB441C0>
<class 'generator'>
[(0, <p class="story">Once upon a time there were three little sisters;and their names were
        <a class="sister" href="http://example.com/elsie" id="link1">
<span>Elsie</span>
</a>
</p>), (1, <body>
<p class="story">Once upon a time there were three little sisters;and their names were
        <a class="sister" href="http://example.com/elsie" id="link1">
<span>Elsie</span>
</a>
</p>
<p class="story">...</p>
</body>), (2, <html><head><title>This Dormouse's story</title></head>
<body>
<p class="story">Once upon a time there were three little sisters;and their names were
        <a class="sister" href="http://example.com/elsie" id="link1">
<span>Elsie</span>
</a>
</p>
<p class="story">...</p>
</body></html>), (3, <html><head><title>This Dormouse's story</title></head>
<body>
<p class="story">Once upon a time there were three little sisters;and their names were
        <a class="sister" href="http://example.com/elsie" id="link1">
<span>Elsie</span>
</a>
</p>
<p class="story">...</p>
</body></html>)]

可以发现返回结果是生成器类型,这里用列表类型输出了其索引和内容,列表中元素就是a节点的祖先元素

兄弟节点

子节点和父节点的获取方式已经介绍完毕,如果想要获取同级节点,也就是兄弟节点,又该怎么办呢?实例如下

from bs4 import BeautifulSoup

html = '''<html><head><title>This Dormouse's story</title></head>
<body>
    <p class="title" name="dormouse"><b>This 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"> <span>Elsie</span></a>
        Hello
        <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>
'''
soup = BeautifulSoup(html,'lxml')
print('Next Sibling',soup.a.next_sibling)
print('Prev Sibling',soup.a.previous_sibling)
print('Next Siblings',list(enumerate(soup.a.next_siblings)))
print('Prev Siblings',list(enumerate(soup.a.previous_siblings)))

 运行结果如下:

Next Sibling
        Hello

Prev Sibling Once upon a time there were three little sisters;and their names were

Next Siblings [(0, '\n        Hello\n        '), (1, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>), (2, '\n        and\n        '), (3, <a class="sister" href="http://example.com/tillie" id="link3">Tillie </a>), (4, ';\n        and they lived at the bottom of a well .')]
Prev Siblings [(0, 'Once upon a time there were three little sisters;and their names were\n        ')]

 可以看到,这里调用了四个属性,next_sibling和previous_sibling分别用于获取节点的下一个和上一个兄弟节点,next_siblings和previous_siblings则分别返回前面和后面的所有节点。

提取信息

前面讲过关联元素节点的选择方法,如果想要获取它们的一些信息,例如文本,属性等,也可以用同样的方法,实例如下:

from bs4 import BeautifulSoup

html = '''<html>
<body>
    
    <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">Bob</a><a 
            href="http://example.com/lacie" class="sister" id="link2">Lacie</a>   
       </p>
       
'''
soup = BeautifulSoup(html,'lxml')
print('Next Sibling')
print(type(soup.a.next_sibling))
print(soup.a.next_sibling)
print('Prev Sibling')
print(type(soup.a.previous_sibling))
print(soup.a.previous_sibling)
print('Parent:')
print(type(soup.a.parents))
print(list(soup.a.parents)[0])
print(list(soup.a.parents)[0].attrs['class'])

运行返回结果:

Next Sibling
<class 'bs4.element.Tag'>
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
Prev Sibling
<class 'bs4.element.NavigableString'>

    Once upon a time there were three little sisters;and their names were

Parent:
<class 'generator'>
<p class="story">
    Once upon a time there were three little sisters;and their names were
        <a class="sister" href="http://example.com/elsie" id="link1">Bob</a><a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
</p>
['story']

 如果返回结果是单个节点,那么可以直接调用string,attrs等属性获取其文本和属性,如果返回结果是包含多个节点的生成器,则可以先将结果转为列表,再从中取出某个元素之后在调用attrs属性即可获取对应节点的文本和属性。

  • 18
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值