已经获得 html-BS4 的解析器:找不到树生成器 ... html-解析器

Already got html-parser for BS4 : Couldn't find a tree builder ... html-parser

我在我的项目目录中安装了 beautifulsoup4,使用 requirment.txt :

requests==2.26.0 beautifulsoup4==4.9.3 html-parser

pip3 install -r requirement.txt --target='local_lib'

我在尝试导入时遇到此错误 运行:

from local_lib.bs4 import BeautifulSoup
soup = Beautifulsoup(html, 'html.parser')


local_lib.bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: html-parser. Do you need to install a parser library?

当我尝试从 bs4 而不是 local_lib.bs4 导入时它起作用了。 但是,我只想为模块使用 local_lib 目录。

为什么我的 local_lib 会出现此错误,我该如何解决?

你做不到。在自己modules/packages之间做import的时候Beautifulsoup4使用绝对import。例如:

https://bazaar.launchpad.net/~leonardr/beautifulsoup/bs4/view/604/bs4/builder/__init__.py#L7

from bs4.element import …

bs4必须是顶层包,不能是子包。要使其成为子包,您应该重写整个源代码并使所有导入相对。类似于 this pull-request to my project Cheetah3 sent with the similar reason:“这使得将 Cheetah 嵌入其他软件包成为可能……