venv error bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requested: lxml.

在Windows系统上使用PyCharm运行GitHub项目时遇到lxml和ssl库相关错误,由于缺失libcrypto-1_1-x64.dll和libssl-1_1-x64.dllDLL文件导致。解决方案包括安装lxml库和从System32或通过安装openssl获取DLL文件,将其放置在anaconda或venv的适当位置。如果问题依然存在,可以尝试使用内置的html.parser解析器代替lxml。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景

在Windows系统上,使用 pycharm 运行自己的github项目https://github.com/birdflyi/db_engines_ranking_table_crawling 的clone 副本(确信在redhat上可执行),在语句 `soup = BeautifulSoup(response_body, 'lxml')` 处发生以下错误:

File "D:\github_repo\db_engines_ranking_table_crawling\venv\lib\site-packages\bs4\__init__.py", line 248, in __init__
    raise FeatureNotFound(
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

原因

未安装`lxml`,import ssl 也发生加载DLL找不到指定的模块错误。

import _ssl # if we can't import it, let the error propagate

ImportError: DLL load failed while importing _ssl: The specified module could not be found.

查阅资料发现venv中缺少的dll:

libcrypto-1_1-x64.dll
libssl-1_1-x64.dll

解决办法(不改动代码的方式)

*注:改动代码的方式可尝试built in python html parser:`soup = BeautifulSoup(s, "html.parser")`,详见python - bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library? - Stack Overflow

不改动代码的方式:

Step1: 安装`lxml`

pip install lxml

  尝试运行,若仍不能执行进入Step2.

Step2: 放入dll

  需要的dll:libcrypto-1_1-x64.dll 和 libssl-1_1-x64.dll

  可以在 “C:\Windows\System32” 下使用搜索功能查找上述两个文件,通常在drivers或者DriverStore文件夹或子文件夹下。

若系统中无相应的dll:

  安装`openssl`:Win32/Win64 OpenSSL Installer for Windows - Shining Light Productions

  找到以上两个dll。

放入anaconda中:

  在anaconda的安装目录中搜索 `_ssl.pyd` 的路径,我的路径如下:

D:\ProgramData\Anaconda3\DLLs
D:\ProgramData\Anaconda3\pkgs\python-3.9.13-h6244533_1\DLLs

  将以上两个dll文件放在`D:\ProgramData\Anaconda3\pkgs\python-3.9.13-h6244533_1\DLLs`下,或者每个下面都丢一份;

  进入pycharm中删除venv,按提示添加interpreter,查看`venv\Scripts\`下是否自动放入了以上两个dll,若未找到,可以手动放入dll;

  将requirements和Step1中的lxml包都安装好,尝试运行即可。

### 解决方案 当使用 `BeautifulSoup` 并指定 `html5lib` 作为解析器时,出现 `bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: html5lib` 错误的主要原因是系统中未安装 `html5lib` 庢[^1]。为了正确解决此问题,需要确保以下条件得到满足: 1. **安装 `html5lib` 库** 如果系统中未安装 `html5lib`,可以通过以下命令进行安装: ```bash pip install html5lib ``` 安装完成后,重新运行代码以验证错误是否解决。 2. **检查 Python 环境** 确保当前使用的 Python 环境与安装 `html5lib` 的环境一致。如果使用了虚拟环境,请激活正确的虚拟环境后再次尝试安装和运行代码。 3. **替代解析器** 如果暂时无法安装 `html5lib` 或遇到其他问题,可以考虑使用其他解析器,例如 `html.parser`(Python 标准库自带)或 `lxml`。以下是使用这些解析器的示例代码: ```python from bs4 import BeautifulSoup # 使用 html.parser soup = BeautifulSoup(html, 'html.parser') # 使用 lxml(需先安装 lxml 库) soup = BeautifulSoup(html, 'lxml') ``` 4. **错误调试** 如果在安装 `html5lib` 后仍然出现类似错误,可能是由于版本兼容性问题。请检查 `beautifulsoup4` 和 `html5lib` 的版本,并确保它们兼容。可以尝试更新相关库: ```bash pip install --upgrade beautifulsoup4 html5lib ``` 5. **完整代码示例** 以下是一个完整的代码示例,展示如何正确使用 `html5lib` 解析器: ```python from bs4 import BeautifulSoup import requests url = "https://example.com" response = requests.get(url) html = response.text # 使用 html5lib 解析器 soup = BeautifulSoup(html, 'html5lib') print(soup.prettify()) ``` --- ### 注意事项 - 如果指定的解析器未安装,`BeautifulSoup` 会抛出 `FeatureNotFound` 错误。因此,在使用非默认解析器时,务必确认相关库已正确安装。 - `pip install parser` 命令无效,因为 `parser` 并不是 `BeautifulSoup` 所需的解析器名称。正确的命令应为 `pip install html5lib` 或 `pip install lxml`[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bird_fly_i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值