### Beautiful Soup 简介
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间.你可能在寻找 Beautiful Soup3 的文档,Beautiful Soup 3 目前已经停止开发,官网推荐在现在的项目中使用Beautiful Soup 4, 移植到BS4
```python
#安装 Beautiful Soup
pip install beautifulsoup4
#安装解析器
Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是 lxml .根据操作系统不同,可以选择下列方法来安装lxml:
$ apt-get install Python-lxml
$ easy_install lxml
$ pip install lxml
另一个可供选择的解析器是纯Python实现的 html5lib , html5lib的解析方式与浏览器相同,可以选择下列方法来安装html5lib:
$ apt-get install Python-html5lib
$ easy_install html5lib
$ pip install html5lib
```
#### 解析器
**下表列出了主要的解析器,以及它们的优缺点,官网推荐使用lxml作为解析器,因为效率更高. 在Python2.7.3之前的版本和Python3中3.2.2之前的版本,必须安装lxml或html5lib, 因为那些Python版本的标准库中内置的HTML解析方法不够稳定.**
<div>
<table>
<thead valign="bottom">
<tr class="row-odd"><th class="head">解析器</th><th class="head">使用方法</th><th class="head">优势</th><th class="head">劣势</th></tr>
</thead>
<tbody valign="top">
<tr class="row-even">
<td>Python标准库</td>
<td><tt class="docutils literal"><span class="pre">BeautifulSoup(markup, <span class="pre">"html.parser")</span></span></tt></td>
<td>
<ul class="first last simple">
<li>Python的内置标准库</li>
<li>执行速度适中</li>
<li>文档容错能力强</li>
</ul>
</td>
<td>
<ul class="first last simple">
<li>Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差</li>
</ul>
</td>
</tr>
<tr class="row-odd">
<td>lxml HTML 解析器</td>
<td><tt class="docutils literal"><span class="pre">BeautifulSoup(markup, <span class="pre">"lxml")</span></span></tt></td>
<td>
<ul class="first last simple">
<li>速度快</li>
<li>文档容错能力强</li>
</ul>
</td>
<td>
<ul class="first last simple">
<li>需要安装C语言库</li>
</ul>