Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库。它能够通过转换器实现惯用的文档导航、查找和修改文档的方式,可以极大的提高工作效率。
    Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据,不需要多少代码就可以写出一个完整的应用程序。
    Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。用户不需要考虑编码方式,只有在文档没有指定一个编码方式时,用户才需说明一下原始编码方式,以保证Beautiful Soup能自动识别编码方式。
    BeautifulSoup已成为和lxml、html6lib一样出色的python解释器,为用户灵活地提供不同的解析策略或强劲的速度。BeautifulSoup3目前已经停止开发,推荐在现在的项目中使用BeautifulSoup4,不过它已经被移植到bs4了,也就是说导入时需要导入bs4。
    安装时使用:
    pip install bs4
    导入时使用:
    import bs4
    beautifulsoup支持标准库中包含的HTML解析器,也支持许多第三方的解析器,例如lxml和html5lib等,这两个解析器可以通过pip进行安装。

    1. from bs4 import BeautifulSoup
    2. # 下面代码示例都是用此文档测试
    3. html = """
    4. <html><head><title>The Dormouse's story</title></head>
    5. <body>
    6. <p class="title"><b>The Dormouse's story</b></p>
    7. <p class="story">Once upon a time there were three little sisters; and their names were
    8. <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
    9. <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    10. <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
    11. and they lived at the bottom of a well.</p>
    12. <p class="story">...</p>
    13. </body>
    14. </html>
    15. """
    16. soup = BeautifulSoup(html, features='lxml')
    17. print(soup)
    1. print(soup)
    2. <html>
    3. <head>
    4. <title>The Dormouse's story</title>
    5. </head>
    6. <body>
    7. <p class="title"><b>The Dormouse's story</b></p>
    8. <p class="story">Once upon a time there were three little sisters; and their names were
    9. <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
    10. <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
    11. <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
    12. and they lived at the bottom of a well.</p>
    13. <p class="story">...</p>
    14. </body>
    15. </html>
    1. soup = BeautifulSoup(html, features='lxml')
    2. tag = soup.a
    3. navstr = tag.string
    4. print(tag)
    5. print(navstr)
    1. <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
    2. Elsie
    1. print(soup.head) # 获取head标签
    2. <head><title>The Dormouse's story</title></head>
    1. print(soup.a.string) # 获取a标签下的文本,只获取第一个
    2. Elsie
    1. print(soup.p.string) # 获取p节点下的内容
    2. The Dormouse's story
    1. print(soup.p.b) # 获取p节点下的b节点
    2. <b>The Dormouse's story</b>
    1. print(soup.find('a')) # find返回匹配到的单个元素
    2. <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
    1. print(soup.find_all('a')) # fina_all返回匹配到的所有元素
    2. [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
    3. <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
    4. <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
    1. print(soup.find_all('a', id='link1')) # 返回匹配到的所有元素中id='link1'的元素
    2. [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]