python 网页解析库 beautifulsoup4 简介

清疚 2021-09-01 06:52 625阅读 0赞

HTML 文档本身是结构化的文本,有一定的规则,通过它的结构可以简化信息提取。于是,就有了lxml、pyquery、BeautifulSoup等网页信息提取库。一般我们会用这些库来提取网页信息。其中,lxml 有很高的解析效率,支持 xPath 语法(一种可以在 HTML 中查找信息的规则语法);pyquery 得名于 jQuery(知名的前端 js 库),可以用类似 jQuery 的语法解析网页。但我们今天要说的,是剩下的这个:BeautifulSoup。

BeautifulSoup(下文简称 bs)翻译成中文就是“美丽的汤”,这个奇特的名字来源于《爱丽丝梦游仙境》(这也是为何在其官网会配上奇怪的插图,以及用《爱丽丝》的片段作为测试文本)。

format_png

bs 最大的特点我觉得是简单易用,不像正则和 xPath 需要刻意去记住很多特定语法,尽管那样会效率更高更直接。对大多数 python 使用者来说,好用会比高效更重要。这也是我自己使用并推荐 bs 的主要原因。

接下来介绍点 bs 的基本方法,让你看完就能用起来。考虑到“只收藏不看党”的阅读体验,先给出一个“嫌长不看版”的总结:

  1. 随anaconda附带,也可以通过pip安装
  2. 指定不同解析器在性能、容错性上会有差异,导致结果也可能不一样
  3. 基本使用流程:通过文本初始化 bs 对象->通过 find/find_all 或其他方法检测信息->输出或保存
  4. 可以迭代式的查找,比如先定位出一段内容,再其上继续检索
  5. 开发时应注意不同方法的返回类型,出错时多看报错、多加输出信息
  6. 官方文档很友好,也有中文,推荐阅读 地址:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

安装

推荐使用pip进行安装:

  1. pip install beautifulsoup4

要注意,包名是beautifulsoup4,如果不加上 4,会是老版本也就是 bs3,它是为了兼容性而存在,目前已不推荐。我们这里说 bs,都是指 bs4。

bs4 也可以直接通过安装 anaconda 获得。

bs 在使用时需要指定一个“解析器”:

  • html.parse- python 自带,但容错性不够高,对于一些写得不太规范的网页会丢失部分内容
  • lxml- 解析速度快,需额外安装
  • xml- 同属 lxml 库,支持 XML 文档
  • html5lib- 最好的容错性,但速度稍慢

这里的 lxml 和 html5lib 都需要额外安装,不过如果你用的是 anaconda,都是一并安装好的。

快速上手

我们就用官网上的文档作例子:

  1. html_doc = """
  2. <html><head><title>The Dormouse's story</title></head>
  3. <body>
  4. <p class="title"><b>The Dormouse's story</b></p>
  5. <p class="story">Once upon a time there were three little sisters; and their names were
  6. <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
  7. <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
  8. <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
  9. and they lived at the bottom of a well.</p>
  10. <p class="story">...</p>
  11. """

使用 bs 的初始化操作,是用文本创建一个 BeautifulSoup 对象,建议手动指定解析器:

  1. from bs4 import BeautifulSoup
  2. soup = BeautifulSoup(html_doc, 'html.parser')

获取其中的某个结构化元素及其属性:

  1. soup.title # title 元素
  2. # <title>The Dormouse's story</title>
  3. soup.p # 第一个 p 元素
  4. # <p class="title"><b>The Dormouse's story</b></p>
  5. soup.p['class'] # p 元素的 class 属性
  6. # ['title']
  7. soup.p.b # p 元素下的 b 元素
  8. # <b>The Dormouse's story</b>
  9. soup.p.parent.name # p 元素的父节点的标签
  10. # body

并不是所有信息都可以简单地通过结构化获取,通常使用 find 和 find_all 方法进行查找:

  1. soup.find_all('a') # 所有 a 元素
  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>]
  5. soup.find(id='link3') # id 为 link3 的元素
  6. # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a
  • find 和 find_all 可以有多个搜索条件叠加,比如find(‘a’, id=’link3’, class_=’sister’)
  • find 返回的是一个bs4.element.Ta****g 对象,这个对象可以进一步进行搜索。如果有多个满足的结果,find只返回第一个;如果没有,返回 None。
  • find_all 返回的是一个由 bs4.element.Tag 对象组成的 list,不管找到几个或是没找到,都是 list。

输出:

  1. x = soup.find(class_='story')
  2. x.get_text() # 仅可见文本内容
  3. # 'Once upon a time there were three little sisters; and their names were\nElsie,\nLacie and\nTillie;\nand they lived at the bottom of a well.'
  4. x.prettify() # 元素完整内容
  5. # '<p class="story">\n Once upon a time there were three little sisters; and their names were\n <a class="sister" href="http://example.com/elsie" id="link1">\n Elsie\n </a>\n ,\n <a class="sister" href="http://example.com/lacie" id="link2">\n Lacie\n </a>\n and\n <a class="sister" href="http://example.com/tillie" id="link3">\n Tillie\n </a>\n ;\nand they lived at the bottom of a well.\n</p>\n'

如果你有前端开发经验,对 CSS 选择器很熟悉,bs 也为你提供了相应的方法:

  1. soup.select('html head title')
  2. # [<title>The Dormouse's story</title>]
  3. soup.select('p > #link1')
  4. # [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]

以上就是 BeautifulSoup 的一个极简上手介绍,对于 bs 能做什么,想必你已有了一个初步认识。如果你要在开发中使用,建议再看下它的官方文档。文档写得很清楚,也有中文版,你只要看了最初的一小部分,就可以在代码中派上用场了。更多的细节可以在使用时进一步搜索具体方法和参数设置。

官方文档中文版地址:

https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

发表评论

表情:
评论列表 (有 0 条评论,625人围观)

还没有评论,来说两句吧...

相关阅读