在 Python 3.5 中导入 bs4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28745153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:40:56  来源:igfitidea点击:

Importing bs4 in Python 3.5

pythonpython-3.xbeautifulsouphtml-parsingpython-3.5

提问by anne_t

I have installed both Python 3.5 and Beautifulsoup4. When I try to import bs4, I get the error below. Is there any fix for that? Or should I just install Python 3.4 instead? Please be very explicit - I am new to programming. Many thanks!

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

采纳答案by alecxe

Update:Starting with 4.4.0, BeautifulSoupis compatible with Python 3.5. Upgrade:

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

pip install --upgrade beautifulsoup4


Old answer:

旧答案:

Because of the changes made for Deprecate strict mode of HTMLParserissue:

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

Issue #15114: the strict mode and argument of HTMLParser, HTMLParser.error, and the HTMLParserError exception have been removed.

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

I'm afraid beautifulSoup4is not compatible with Python 3.5 at the moment. Use Python 3.4.

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

回答by user2931216

I've sent the author a followup about this bug. If you want to install BeautifulSoup on Python 3.5a, I've uploaded a working patch of the source code to github.

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

https://github.com/jjangsangy/BeautifulSoup4

https://github.com/jjangsangy/BeautifulSoup4

You can install it using setup.pyor just copy & paste this code into terminal.

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

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

I'm assuming here that since you're trying out 3.5a your python interpreter is installed with proper user permissions for your site-packagesdirectory so no sudo invocation is necessary.

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

回答by AnilRedshift

Update: BeautifulSoup 4.4.0 has been updated to be python3.5 compatible, so a pip install --upgrade beautifulsoup4should do the trick if you are still hitting this issue.

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