场景
在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包都安装好,尝试运行即可。