python - bs4.FeatureNotFound : Couldn't find a tre

...
soup = BeautifulSoup(html, "lxml")
File "/Library/Python/2.7/site-packages/bs4/__init__.py", line 152, in __init__
% ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

我的终端上的上述输出。我在 Mac OS 10.7.x 上。我有 Python 2.7.1,并关注 this tutorial获得 Beautiful Soup 和 lxml,它们都已成功安装并使用单独的测试文件 located here .在导致此错误的 Python 脚本中,我包含了这一行: 从 pageCrawler 导入 comparePages 在 pageCrawler 文件中,我包含了以下两行: 从 bs4 导入 BeautifulSoup 从 urllib2 导入 urlopen

任何帮助找出问题是什么以及如何解决问题将不胜感激。

最佳答案

我怀疑这与 BS 用于读取 HTML 的解析器有关。他们document is here ,但如果你像我一样(在 OSX 上),你可能会遇到一些需要做一些工作的事情:

您会注意到,在上面的 BS4 文档页面中,他们指出默认情况下 BS4 将使用 Python 内置的 HTML 解析器。假设您在 OSX 中,Apple 捆绑的 Python 版本是 2.7.2,它对字符格式不宽容。我遇到了同样的问题,所以我升级了我的 Python 版本来解决它。在 virtualenv 中执行此操作将最大限度地减少对其他项目的干扰。

如果这样做听起来很痛苦,您可以切换到 LXML 解析器:

pip install lxml

然后试试:

soup = BeautifulSoup(html, "lxml")

根据您的情况,这可能已经足够了。我发现这很烦人,需要升级我的 Python 版本。使用 virtualenv,you can migrate your packages相当容易。

关于python - bs4.FeatureNotFound : Couldn't find a tree builder with the features you requested: lxml. 需要安装解析器库吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24398302/

相关文章:

python - 使用Python在SQLite中插入行后如何检索插入的ID?

python - ConfigParser 中的列表

python - 未找到 Virtualenv 命令

linux - 什么包包括 AB Ubuntu 中的 Apache 服务器基准测试工具

linux - 使用 bash 为文件添加文件扩展名

linux - 仅使用 shell 脚本从文本文件中获取特定行

python - 如何在不导入的情况下检查 Python 模块是否存在

python - 如何在python中打印百分比值?

linux - 选择 Linux I/O 调度程序

ruby - 找不到 gem 命令