top of page

运行 BeautifulSoup 时发生 FeatureNotFound 错误

  • 2天前
  • 讀畢需時 1 分鐘

在AWS Lambda python3.9中如下运行BeautifulSoup时

soup = BeautifulSoup(response.text, 'lxml')

把发生以下错误时的应对方法记录下来以备忘。

[ERROR] FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

应对1:安装 lxml

从错误消息判断,我以为安装lxml的解析器就能解决,于是安装了lxml库

pip install lxml -t .

但现象并未解决……

应对2:用 html5lib 代替 lxml

BeautifulSoup除lxml外还支持html5lib作为解析器,于是尝试使用html5lib。

如果尚未安装html5lib,则先安装。

pip install html5lib -t .

把BeautifulSoup的调用部分修改如下。

soup = BeautifulSoup(response.text, 'html5lib')

运行之后问题解决了。

并没有非用lxml不可的理由,因此我就以此作为解决方案了。

 
 
 

留言


© Copyright ROBIN planning LLC.

bottom of page