Python 3.4 中的 BeautifulSoup 无效语法(在 2to3.py 之后) [英] BeautifulSoup invalid syntax in Python 3.4 (after 2to3.py)

查看:16
本文介绍了Python 3.4 中的 BeautifulSoup 无效语法(在 2to3.py 之后)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<预><代码>>>>从 bs4 导入 BeautifulSoup回溯(最近一次调用最后一次):文件<pyshell#0>",第 1 行,在 <module> 中从 bs4 导入 BeautifulSoup文件C:Python34s4\__init__.py",第 30 行,在 <module> 中从 .builder 导入 builder_registry, ParserRejectedMarkup文件C:Python34s4uilder\__init__.py",第 4 行,在 <module>从 bs4.element 导入(文件C:Python34s4element.py",第 1213 行打印 '运行 CSS 选择器%s"' % 选择器^语法错误:调用打印"时缺少括号

有什么想法吗?

解决方案

BeautifulSoup 4 不需要需要手动转换才能在 Python 3 上运行.您正在尝试运行仅与 Python 2 兼容的代码;看来您未能正确转换代码库.

来自 BeautifulSoup 4 主页:

<块引用>

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

现在抛出异常的行应该阅读:

print('运行 CSS 选择器 "%s"' % 选择器)

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

pip install beautifulsoup4

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

python3.4 -m pip install beautifulsoup4

或使用easy_install:

easy_install beautifulsoup4

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

python3.4 setup.py install

让安装程序为您正确转换代码库;转换后的代码将复制到您的 Python 设置中.运行命令后可以丢弃下载的源目录,参见安装方式/em>.

或者,运行:

python3.4 setup.py 构建

并复制到 build/lib 目录.同样,不要不要使用原始源目录,因为它保持不变.

I am trying to install Beautiful Soup 4 in Python 3.4. I installed it from the command line, (got the invalid syntax error because I had not converted it), ran the 2to3.py conversion script to bs4 and now I get a new invalid syntax error.

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

Any ideas?

解决方案

BeautifulSoup 4 does not need manual converting to run on Python 3. You are trying to run code only compatible with Python 2 instead; it appears you failed to correctly convert the codebase.

From the BeautifulSoup 4 homepage:

Beautiful Soup 4 works on both Python 2 (2.6+) and Python 3.

The line now throwing the exception should read:

print('Running CSS selector "%s"' % selector)

The codebase does use Python 2 syntax, but the setup.py installer converts this for you to compatible Python 3 syntax. Make sure to install the project with pip:

pip install beautifulsoup4

or using the pip version bundled with Python 3.4:

python3.4 -m pip install beautifulsoup4

or using easy_install:

easy_install beautifulsoup4

If you downloaded just the tarball, at the very least run

python3.4 setup.py install

to have the installer correctly convert the codebase for you; the converted code is copied into your Python setup. You can discard the downloaded source directory after running the command, see How installation works.

Alternatively, run:

python3.4 setup.py build

and copy across the build/lib directory. Again, do not use the original source directory as it is left untouched.

这篇关于Python 3.4 中的 BeautifulSoup 无效语法(在 2to3.py 之后)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆