...
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

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


当前回答

pip安装lxml,然后将xml保存在soup = BeautifulSoup(URL, "xml")在Mac上完成了这项工作。

其他回答

尽管BeautifulSoup默认支持HTML解析器 如果您想使用任何其他第三方Python解析器,则需要安装该外部解析器,如(lxml)。

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

但是如果你没有指定任何解析器作为参数,你会得到一个没有指定解析器的警告。

soup_object= BeautifulSoup(markup) #Warnning

要使用任何其他外部解析器,您需要安装它,然后需要指定它。就像

pip install lxml

soup_object= BeautifulSoup(markup, 'lxml') # C dependent parser 

外部解析器依赖于c和python,这可能有一些优点和缺点。

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

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

pip安装lxml,然后将xml保存在soup = BeautifulSoup(URL, "xml")在Mac上完成了这项工作。

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

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

这个方法对我很有效。我想说的是,我是在虚拟环境中尝试这个方法的。第一:

pip install --upgrade bs4

其次,我使用了:

html.parser

而不是

html5lib