【问题标题】:Just installed BeautifulSoup Python 3.3.0刚刚安装 BeautifulSoup Python 3.3.0
【发布时间】:2012-10-17 07:28:19
【问题描述】:

有谁知道如何解决它。 我使用的是 Mac OS 10.8.2

>>> from bs4 import BeautifulSoup
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/bs4/__init__.py", line 359
    print soup.prettify()
             ^
SyntaxError: invalid syntax

【问题讨论】:

  • 尝试从终端运行导入语句,你会得到什么?
  • 你是如何安装 bs4 的?鉴于您显然不知道 pip 是什么,我想知道您是否以某种不正确的方式得到它……
  • 我安装了它,可以确认BeautifulSoup的最新版本不存在这个问题,所以你错误地安装了一个旧的Python 2 only版本。

标签: python python-3.x beautifulsoup


【解决方案1】:

在 Python 3 中,print 是一个函数;应该是:

print(soup.prettify())

正确安装bs4,如果是错误则使用更新的版本。 beautifulsoup4==4.1.3 在 Python 3.3 上运行良好。

【讨论】:

  • 我很惊讶BeautifulSoup在正式支持Python 3.x时出现了这个问题。似乎猴子补丁 bs4 是唯一的出路
  • 谢谢,我使用的是 4.1.3。应该是bug
  • @IanZhaoqiLeng:我查看了站点包中安装的 4.1.3 源代码;它正确使用()。我已经使用pip install beautifulsoup4 安装了它。
  • 我使用了“pip install beautifulsoup4”,但它显示“-bash: pip: command not found”
  • 您可能想要pip-3.3,而不是pip(因为后者将安装到2.7)。你可能还没有,在这种情况下你必须easy_install-3.3 pip。对于 3.3,您甚至可能还没有 easy_install,在这种情况下,您必须手动下载并安装 distribute。 (对于现有的 Mac 2.7,你肯定有 easy_install 但没有 pip。安装 pip 应该是你做的第一件事。)
【解决方案2】:

https://github.com/il-vladislav/BeautifulSoup4 复制到 Lib 目录即可。它是 Python 3.3 的固定版本

【讨论】:

    【解决方案3】:

    Beautiful Soup 4 适用于 Python 2 (2.6+) 和 Python 3。

    Beautiful Soup 在 MIT 许可下获得许可,因此您也可以下载 tarball,将 bs4/ 目录拖放到几乎任何 Python 应用程序(或您的库路径)中,然后立即开始使用它。如果你想在 Python 3 下这样做,你需要使用 2to3 手动转换代码。

    http://www.crummy.com/software/BeautifulSoup/

    使用 python ../python33/Tools/Script/2to3.py -w beautifulsoup4-4.3.2 没问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      • 2016-04-20
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 2012-10-04
      相关资源
      最近更新 更多