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

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


当前回答

我修复了以下变化

之前更改

soup = BeautifulSoup(r.content, 'html5lib' )
print (soup.prettify())

后改变

soup = BeautifulSoup(r.content, features='html')
print(soup.prettify())

我的代码正常工作

其他回答

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

# 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 

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


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

BS4默认情况下需要HTML文档。因此,它将XML文档解析为HTML文档。在构造函数中传递features="xml"作为参数。它解决了我的问题。

我的解决方案是从conda中删除lxml,然后用pip重新安装它。

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

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