使用 Beautiful Soup 时出现错误“AttributeError 'collections' has no attribute 'Callable'”

Tan*_*010 29 python beautifulsoup

我按照所有步骤安装 Beautiful Soup,但仍然出现此错误:

\n
\n

AttributeError:模块“collections”没有属性“Callable”

\n
\n

堆栈跟踪

\n

我正在使用Python\xc2\xa03.10。

\n

ali*_*afi 54

collections.Callable移至collections.abc.Callablepython 3.10+ 中。collections一个巧妙的解决方案是在导入问题库之前将引用添加回。

import collections
collections.Callable = collections.abc.Callable

from bs4 import BeautifulSoup # for example
Run Code Online (Sandbox Code Playgroud)


小智 26

这个解决方案对我有用:

  1. 导航到Python310\Lib\site-packages\pyreadline文件浏览器中。
  2. py3k_compat.py在文本编辑器中打开。
  3. 将应该 return 的第 8 行更改为isinstance(x, collections.Callable)return isinstance(x, collections.abc.Callable)
  4. 保存、退出然后重试。


小智 10

您可以卸载 Python 包pyreadline并安装pyreadline3,这是一个更新的分支!


小智 7

我得到了同样的属性错误。

\n

然后我查看了 Python lib \xe2\x86\x92集合文件夹的内部。它有ABC(抽象基类)。

\n

因此,通过在编辑器中打开/bs4/elements.py并将collections.callable替换为collections.abc.callable来解决错误。因此可以轻松访问可调用属性。

\n


小智 2

Beautiful Soup 不适用于 3.10。

重新安装3.9。