...
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,并遵循本教程获得了Beautiful Soup和lxml,它们都成功安装了,并与位于这里的单独测试文件一起工作。在导致此错误的Python脚本中,我包含了这一行: 导入comparePages 在pageCrawler文件中,我包含了以下两行代码: 从bs4导入BeautifulSoup 从urllib2导入urlopen

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


当前回答

对于安装了bs4的基本开箱即用的python,您可以使用

soup = BeautifulSoup(html, "html5lib")

如果你想使用formatter='xml',那么你需要

pip3 install lxml

soup = BeautifulSoup(html, features="xml")

其他回答

在一些参考文献中,使用第二个而不是第一个:

soup_object= BeautifulSoup(markup,'html-parser')
soup_object= BeautifulSoup(markup,'html.parser')

在我的例子中,我有一个过时版本的lxml包。所以我更新了它,这就解决了这个问题。

sudo python3 -m pip install lxml --upgrade

我在pycharm中使用python 3.8。我假设您在开始工作之前没有安装“lxml”。这就是我所做的:


进入“文件->设置” 在左侧设置菜单栏选择“Python解释器”,选择“Python解释器”。 单击包列表上的“+”图标。 搜索“lxml”。 点击“可用软件包”窗口左下方的“安装软件包”。

实际上是其他作品中提到的三个选项。

# 1. 
soup_object= BeautifulSoup(markup,"html.parser") #Python HTML parser

# 2. 
pip install lxml
soup_object= BeautifulSoup(markup,'lxml') # C dependent parser 

# 3.
pip install html5lib
soup_object= BeautifulSoup(markup,'html5lib') # C dependent parser 

不要使用lxml,而是使用html。解析器,你可以使用这段代码:

soup = BeautifulSoup(html, 'html.parser')