16

我已经安装了 Python 3.5 和 Beautifulsoup4。当我尝试导入 bs4 时,出现以下错误。有什么解决办法吗?还是我应该只安装 Python 3.4?请非常明确 - 我是编程新手。非常感谢!

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python 3.5\lib\sit-packages\bs4\__init__.py", line 30, in    <module>
   from .builder import builder_registry, ParserRejectionMarkup
  File "C:\Python 3.5\lib\sit-packages\bs4\__init__.py", line 308, in <module>
   from . import _htmlparser
  File "C:\Python 3.5\lib\sit-packages\bs4\_htmlparser.py", line 7, in <module>
   from html.parser import ( 
ImportError: cannot import name 'HTMLParseError'
4

3 回答 3

29

更新:从 4.4.0 开始,BeautifulSoup与 Python 3.5 兼容。升级:

pip install --upgrade beautifulsoup4

老答案:

由于对HTMLParser问题的弃用严格模式所做的更改:

问题 #15114:HTMLParser、HTMLParser.error 和 HTMLParserError 异常的严格模式和参数已被删除。

恐怕beautifulSoup4目前与 Python 3.5 不兼容。使用 Python 3.4。

于 2015-02-26T14:45:39.153 回答
19

更新:BeautifulSoup 4.4.0 已更新为与 python3.5 兼容,因此pip install --upgrade beautifulsoup4如果您仍然遇到此问题,应该可以解决问题。

于 2015-09-15T14:59:00.077 回答
4

我已向作者发送了有关此错误的后续信息。如果您想在 Python 3.5a 上安装 BeautifulSoup,我已将源代码的工作补丁上传到 github。

https://github.com/jjangsangy/BeautifulSoup4

setup.py您可以使用或只是将此代码复制并粘贴到终端中来安装它。

git clone https://github.com/jjangsangy/BeautifulSoup4 \
    && cd BeautifulSoup4 \
    && python3.5 setup.py install

我在这里假设,由于您正在尝试 3.5a,因此您的 python 解释器已安装为您的site-packages目录具有适当的用户权限,因此不需要 sudo 调用。

于 2015-03-15T09:14:16.633 回答