安装 lxml 后出现“bs4.FeatureNotFound:无法找到具有您请求的功能的树构建器:lxml”

Jos*_*sky 8 python lxml pip beautifulsoup

我正在尝试使用 Beautiful Soup 来解析 XML 文档。这是我实例化对象的代码BeautifulSoup

with open(filename, encoding='utf-8') as f_:
    content = f_.read()
xml_cont = BeautifulSoup(content, 'lxml')
Run Code Online (Sandbox Code Playgroud)

当我运行我的代码时,我收到以下错误:

  File "[omitted]", line 13, in [omitted]
    xml_cont = BeautifulSoup(content, 'lxml')
  File "/Users/Josh/Library/Python/3.7/lib/python/site-packages/bs4/__init__.py", line 228, 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?
Run Code Online (Sandbox Code Playgroud)

我快速搜索了一下,发现我需要lxml用 pip 安装。我就这么做了。

pip3 install lxml
Run Code Online (Sandbox Code Playgroud)

但是,我仍然收到错误!有什么想法吗?

小智 2

我已经用 pip 尝试了很多库,但对我不起作用。只有当我从 IDE (Pycharm) 安装库时它才有效。

以 open(文件名) 作为文件: soup = BeautifulSoup(file, "lxml")

lxml库安装