• python爬虫入门(六)BeautifulSoup使用


    简单来说,BeautifulSoup 就是 Python 的一个 HTML 或 XML 的解析库,我们可以用它来方便地从网页中提取数据,官方的解释如下:

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

    解析器

    Beautiful Soup 在解析时实际上依赖解析器,它除了支持 Python 标准库中的 HTML 解析器外,还支持一些第三方解析器(比如 lxml)。

    解析器使用方法优势劣势
    Python 标准库BeautifulSoup(markup, “html.parser”)Python 的内置标准库、执行速度适中 、文档容错能力强Python 2.7.3 or 3.2.2) 前的版本中文容错能力差
    LXML HTML 解析器BeautifulSoup(markup, “lxml”)速度快、文档容错能力强需要安装 C 语言库
    LXML XML 解析器BeautifulSoup(markup, “xml”)速度快、唯一支持 XML 的解析器需要安装 C 语言库
    html5libBeautifulSoup(markup, “html5lib”)最好的容错性、以浏览器的方式解析文档、生成 HTML5 格式的文档速度慢、不依赖外部扩展

    如果使用 lxml,那么在初始化 Beautiful Soup 时,可以把第二个参数改为 lxml 即可:

    from bs4 import BeautifulSoup
    soup = BeautifulSoup('

    Hello

    ', 'lxml') print(soup.p.string)
    • 1
    • 2
    • 3

    基本使用

    用实例来看看 Beautiful Soup 的基本用法:

    html = """
    The Dormouse's story
    
    

    The Dormouse's story

    Once upon a time there were three little sisters; and their names were , Lacie and Tillie; and they lived at the bottom of a well.

    ...

    """ from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'lxml') print(soup.prettify()) print(soup.title.string)
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    运行结果:

    
     
      
       The Dormouse's story
      
     
     
      

    The Dormouse's story

    Once upon a time there were three little sisters; and their names were , Lacie and Tillie ; and they lived at the bottom of a well.

    ...

    The Dormouse's story
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    运行结果:

    
     
      
       The Dormouse's story
      
     
     
      

    The Dormouse's story

    Once upon a time there were three little sisters; and their names were , Lacie and Tillie ; and they lived at the bottom of a well.

    ...

    The Dormouse's story
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    结果如下:

    
     
      
       The Dormouse's story
      
     
     
      

    The Dormouse's story

    Once upon a time there were three little sisters; and their names were , Lacie and Tillie ; and they lived at the bottom of a well.

    ...

    The Dormouse's story
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    运行结果:

    
     
      
       The Dormouse's story
      
     
     
      

    The Dormouse's story

    Once upon a time there were three little sisters; and their names were , Lacie and Tillie ; and they lived at the bottom of a well.

    ...

    The Dormouse's story
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

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

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

    调用 soup.title.string,这实际上是输出 HTML 中 title 节点的文本内容。所以,soup.title 可以选出 HTML 中的 title 节点,再调用 string 属性就可以得到里面的文本了。可以通过简单调用几个属性完成文本提取,比起XPath更加方便。

    节点选择器

    选择元素

    依然选用刚才的 HTML 代码:

    soup = BeautifulSoup(html, 'lxml')
    print(soup.title)
    print(type(soup.title))
    print(soup.title.string)
    print(soup.head)
    print(soup.p)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    运行结果:

    The Dormouse's story
    
    The Dormouse's story
    The Dormouse's story
    

    The Dormouse's story

    • 1
    • 2
    • 3
    • 4
    • 5

    首先打印输出 title 节点的选择结果,输出结果正是 title 节点加里面的文字内容。接下来,输出它的类型,是 bs4.element.Tag 类型,这是 Beautiful Soup 中一个重要的数据结构。经过选择器选择后,选择结果都是这种 Tag 类型。Tag 具有一些属性,比如 string 属性,调用该属性,可以得到节点的文本内容,所以接下来的输出结果正是节点的文本内容。

    接着尝试选择了 head 节点,结果也是节点加其内部的所有内容。最后,选择了 p 节点。不过这次情况比较特殊,我们发现结果是第一个 p 节点的内容,后面的几个 p 节点并没有选到。也就是说,当有多个节点时,这种选择方式只会选择到第一个匹配的节点,其他的后面节点都会忽略。

    提取信息

    获取名称

    可以利用 name 属性获取节点的名称。这里还是以上面的文本为例,选取 title 节点,然后调用 name 属性就可以得到节点名称:

    print(soup.title.name)
    
    • 1

    获取属性

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

    print(soup.p.attrs)
    print(soup.p.attrs['name'])
    
    • 1
    • 2

    运行结果:

    {'class': ['title'], 'name': 'dromouse'}
    dromouse
    
    • 1
    • 2

    attrs 的返回结果是字典形式,它把选择的节点的所有属性和属性值组合成一个字典。接下来,如果要获取 name 属性,就相当于从字典中获取某个键值,只需要用中括号加属性名就可以了。

    还有一种更简单的获取方式:可以不用写 attrs,直接在节点元素后面加中括号,传入属性名就可以获取属性值了。

    print(soup.p['name'])
    print(soup.p['class'])
    
    • 1
    • 2

    获取内容

    利用 string 属性获取节点元素包含的文本内容。

    嵌套选择

    在上面的例子中,我们知道每一个返回结果都是 bs4.element.Tag 类型,它同样可以继续调用节点进行下一步的选择。比如,我们获取了 head 节点元素,我们可以继续调用 head 来选取其内部的 head 节点元素:

    print(soup.head.title)
    print(type(soup.head.title))
    print(soup.head.title.string)
    
    The Dormouse's story
    
    The Dormouse's story
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

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

    关联选择

    有时候不能做到一步就选到想要的节点元素,需要先选中某一个节点元素,然后以它为基准再选择它的子节点、父节点、兄弟节点等。

    子节点和子孙节点

    选取节点元素之后,如果想要获取它的直接子节点,可以调用 contents 属性。

    html = """
    
        
            The Dormouse's story
        
        
            

    Once upon a time there were three little sisters; and their names were Elsie Lacie and Tillie and they lived at the bottom of a well.

    ...

    """ from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'lxml') print(soup.p.contents)
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    运行结果:

    ['\n            Once upon a time there were three little sisters; and their names were\n            ', 
    
    Elsie
    , 
    '\n',
    Lacie, 
    ' \n            and\n            ', 
    Tillie, 
    '\n            and they lived at the bottom of a well.\n        ']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    返回结果是列表形式。p 节点里既包含文本,又包含节点,最后会将它们以列表形式统一返回。

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

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

    for i, child in enumerate(soup.p.children):
        print(i, child)
    
    • 1
    • 2
    
    0 
                Once upon a time there were three little sisters; and their names were
    
    1 
    Elsie
    
    2 
    
    3 Lacie
    4  
                and
    
    5 Tillie
    6 
                and they lived at the bottom of a well.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    要得到所有的子孙节点的话,可以调用 descendants 属性:

    print(soup.p.descendants)
    for i, child in enumerate(soup.p.descendants):
        print(i, child)
    
    • 1
    • 2
    • 3
    
    0 
                Once upon a time there were three little sisters; and their names were
    
    1 
    Elsie
    
    2 
    
    3 Elsie
    4 Elsie
    5 
    
    6 
    
    7 Lacie
    8 Lacie
    9  
                and
    
    10 Tillie
    11 Tillie
    12 
                and they lived at the bottom of a well.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    此时返回结果还是生成器。遍历输出一下可以看到,这次的输出结果就包含了 span 节点。descendants 会递归查询所有子节点,得到所有的子孙节点。

    父节点和祖先节点

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

    html = """
    
        
            The Dormouse's story
        
        
            

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

    ...

    """ from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'lxml') print(soup.a.parent)
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

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

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    输出结果便是 p 节点及其内部的内容。

    如果想获取所有的祖先节点,可以调用 parents 属性:

    html = """
    
        
            

    Elsie

    """ from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'lxml') print(type(soup.a.parents)) print(list(enumerate(soup.a.parents)))
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    html = """
    
        
            

    Elsie

    """ from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'lxml') print(type(soup.a.parents)) print(list(enumerate(soup.a.parents)))
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    
    [(0, 

    Elsie

    ), (1,

    Elsie

    ), (2,

    Elsie

    ), (3,

    Elsie

    )]
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    兄弟节点

    如果要获取同级的节点(也就是兄弟节点)

    html = """
    
        
            

    Once upon a time there were three little sisters; and their names were Elsie Hello Lacie and Tillie and they lived at the bottom of a well.

    """ from bs4 import BeautifulSoup 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)))
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    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, Lacie), (2, ' \n            and\n            '), (3, Tillie), (4, '\n            and they lived at the bottom of a well.\n        ')]
    Prev Siblings [(0, '\n            Once upon a time there were three little sisters; and their names were\n            ')]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    next_sibling 和 previous_sibling 分别获取节点的下一个和上一个兄弟元素,next_siblings 和 previous_siblings 则分别返回后面和前面的兄弟节点。

    提取信息

    如果想要获取它们的一些信息,比如文本、属性等,也用同样的方法

    html = """
    
        
            

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

    """ from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'lxml') print('Next Sibling:') print(type(soup.a.next_sibling)) print(soup.a.next_sibling) print(soup.a.next_sibling.string) print('Parent:') print(type(soup.a.parents)) print(list(soup.a.parents)[0]) print(list(soup.a.parents)[0].attrs['class'])
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    Next Sibling:
    
    Lacie
    Lacie
    Parent:
    
    

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

    ['story']
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    如果返回结果是单个节点,那么可以直接调用 string、attrs 等属性获得其文本和属性;如果返回结果是多个节点的生成器,则可以转为列表后取出某个元素,然后再调用 string、attrs 等属性获取其对应节点的文本和属性。

    方法选择器

    Beautiful Soup 还为我们提供了一些查询方法,比如 find_all 和 find 等,调用它们,然后传入相应的参数,就可以灵活查询了。

    find_all

    find_all,查询所有符合条件的元素,可以给它传入一些属性或文本来得到符合条件的元素.

    find_all(name , attrs , recursive , text , **kwargs)
    
    • 1

    find

    除了 find_all 方法,还有 find 方法,只不过 find 方法返回的是单个元素,也就是第一个匹配的元素,而 find_all 返回的是所有匹配的元素组成的列表。

    find_parents 和 find_parent:前者返回所有祖先节点,后者返回直接父节点。

    find_next_siblings 和 find_next_sibling:前者返回后面所有的兄弟节点,后者返回后面第一个兄弟节点。

    find_previous_siblings 和 find_previous_sibling:前者返回前面所有的兄弟节点,后者返回前面第一个兄弟节点。

    find_all_next 和 find_next:前者返回节点后所有符合条件的节点,后者返回第一个符合条件的节点。

    find_all_previous 和 find_previous:前者返回节点前所有符合条件的节点,后者返回第一个符合条件的节点。

    CSS选择器

    使用 CSS 选择器,只需要调用 select 方法,传入相应的 CSS 选择器即可。

    print(soup.select('.panel .panel-heading'))
    print(soup.select('ul li'))
    print(soup.select('#list-2 .element'))
    print(type(soup.select('ul')[0]))
    
    • 1
    • 2
    • 3
    • 4

    select(‘ul li’) 则是选择所有 ul 节点下面的所有 li 节点,结果便是所有的 li 节点组成的列表。

    嵌套选择

    select 方法同样支持嵌套选择,例如我们先选择所有 ul 节点,再遍历每个 ul 节点选择其 li 节点。

    for ul in soup.select('ul'):
        print(ul.select('li'))
    
    • 1
    • 2

    获取属性

    节点类型是 Tag 类型,所以获取属性还可以用原来的方法。

    for ul in soup.select('ul'):
        print(ul['id'])
        print(ul.attrs['id'])
    
    • 1
    • 2
    • 3

    获取文本

    可以用前面所讲的 string 属性。还有一个方法,那就是 get_text。

    for li in soup.select('li'):
        print('Get Text:', li.get_text())
        print('String:', li.string)
    
    • 1
    • 2
    • 3
  • 相关阅读:
    Zabbix之SNMP的OID获取
    SuperMap iClient3D for WebGL教程(S3MTilesLayer)- 显示优化设置
    阿里云短信服务接入流程
    1004. 最大连续1的个数 III ●●
    一篇文章带你了解最近很火的RunnerGo测试平台
    IDEA 高版本 PlantUML 插件默认主题修改
    PyTorch - Sequential和ModuleList
    【react native】模拟mock接口
    python使用泛型
    Webpack中的plugin插件机制
  • 原文地址:https://blog.csdn.net/qq_40369277/article/details/134019183