【问题标题】:Importing bs4 in Python 3.5在 Python 3.5 中导入 bs4
【发布时间】:2015-04-28 23:44:48
【问题描述】:

我已经安装了 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'

【问题讨论】:

    标签: python python-3.x beautifulsoup html-parsing python-3.5


    【解决方案1】:

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

    pip install --upgrade beautifulsoup4
    

    旧答案:

    由于对Deprecate strict mode of HTMLParser 问题所做的更改:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 可能是特定于 pip 版本的,尝试pip3.5 install --upgrade beautifulsoup4 否则它将升级 bs4 为您的默认 python 版本(大多数 *nix 上为 2.7)
      • sudo pip3 install --upgrade beautifulsoup4
      【解决方案3】:

      我已向作者发送了有关此错误的后续信息。如果您想在 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 调用。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-30
      相关资源
      最近更新 更多