BeautifulSoup 的安装 | 珊瑚贝

BeautifulSoup 是 Python 的一个 HTML 或 XML 的解析库,我们可以用它来方便地从网页中提取数据,它拥有强大的 API 和多样的解析方式,本节我们了解下它的安装方式。

相关链接

准备工作

BeautifulSoup 的 HTML 和 XML 解析器是依赖于 lxml 库的,所以在此之前请确保已经成功安装好了 lxml 库,具体的安装方式参见 lxml 的安装:https://setup.scrape.center/lxml

安装方法

pip 安装

目前 BeautifulSoup 的最新版本是 4.x 版本,之前的版本已经停止开发了,推荐使用 pip3 来安装,安装命令如下:

1
pip3 install beautifulsoup4

命令执行完毕之后即可完成安装。

wheel 安装

当然也可以从 PyPi 下载 wheel 文件安装,链接如下:
https://pypi.python.org/pypi/beautifulsoup4

然后 pip3 安装 Wheel 文件即可。

验证安装

安装完成之后可以运行下方的代码验证一下。

1
2
3
from bs4 import BeautifulSoup
soup = BeautifulSoup('<p>Hello</p>', 'lxml')
print(soup.p.string)

运行结果:

1
Hello

如果运行结果一致则证明安装成功。

注意在这里我们虽然安装的是 beautifulsoup4 这个包,但是在引入的时候是引入的 bs4,这是因为这个包源代码本身的库文件夹名称就是 bs4,所以安装完成之后,这个库文件夹就被移入到我们本机 Python3 的 lib 库里,所以识别到的库文件名称就叫做 bs4,所以我们引入的时候就引入 bs4 这个包。

因此,包本身的名称和我们使用时导入的包的名称并不一定是一致的。

来源:https://cuiqingcai.com/31030.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_11466.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?