Python 3.4中的BeautifulSoup无效语法(在2to3.py之后)

len*_*eth 3 python beautifulsoup python-3.x python-3.4

我想在Python 3.4中安装Beautiful Soup 4.我从命令行安装它(得到了无效的语法错误,因为我没有转换它),运行2to3.py转换脚本到bs4现在我得到一个新的无效语法错误.

>>> from bs4 import BeautifulSoup
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
    from bs4 import BeautifulSoup
  File "C:\Python34\bs4\__init__.py", line 30, in <module>
    from .builder import builder_registry, ParserRejectedMarkup
  File "C:\Python34\bs4\builder\__init__.py", line 4, in <module>
    from bs4.element import (
  File "C:\Python34\bs4\element.py", line 1213
    print 'Running CSS selector "%s"' % selector
                                    ^
SyntaxError: Missing parentheses in call to 'print'
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Mar*_*ers 8

BeautifulSoup 4并没有需要手动转换到Python 3的你正在尝试运行的代码只能与Python 2而不是兼容运行; 您似乎无法正确转换代码库.

来自BeautifulSoup 4主页:

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

现在抛出异常的行为:

print('Running CSS selector "%s"' % selector)
Run Code Online (Sandbox Code Playgroud)

代码库确实使用Python 2语法,但setup.py安装程序会将此转换为兼容的Python 3语法.确保安装项目pip:

pip install beautifulsoup4
Run Code Online (Sandbox Code Playgroud)

或使用pip与Python 3.4捆绑在一起的版本:

python3.4 -m pip install beautifulsoup4
Run Code Online (Sandbox Code Playgroud)

或使用easy_install:

easy_install beautifulsoup4
Run Code Online (Sandbox Code Playgroud)

如果你只下载了tarball,至少可以运行

python3.4 setup.py install
Run Code Online (Sandbox Code Playgroud)

让安装程序正确地为您转换代码库; 转换后的代码将复制到Python设置中.您可以在运行命令后丢弃下载的源目录,请参阅安装的工作原理.

或者,运行:

python3.4 setup.py build
Run Code Online (Sandbox Code Playgroud)

并在build/lib目录中复制.同样,也不能因为它是原封不动使用原来的源目录.