FAQ
Dear friends:

I got a import error when I use Python 3.2 to import BeautifulSoup 3.2.0 .
Is there any differences between Python 3.2 and other version? This is my first time to use Python3.2 .
And the error message will be as below.

from BeautifulSoup import BeautifulSoup
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
from BeautifulSoup import BeautifulSoup
File "C:\Python32\BeautifulSoup.py", line 448
raise AttributeError, "'%s' object has no attribute '%s'" % (self.__class__.__name__, attr)
^
SyntaxError: invalid syntax



And the error place point to BeautifulSoup

class NavigableString(unicode, PageElement):
def __new__(cls, value):
"""Create a new NavigableString.
When unpickling a NavigableString, this method is called with
the string in DEFAULT_OUTPUT_ENCODING. That encoding needs to be
passed in to the superclass's __new__ or the superclass won't know
how to handle non-ASCII characters.
"""
if isinstance(value, unicode):
return unicode.__new__(cls, value)
return unicode.__new__(cls, value, DEFAULT_OUTPUT_ENCODING)
def __getnewargs__(self):
return (NavigableString.__str__(self),)
def __getattr__(self, attr):
"""text.string gives you text. This is for backwards
compatibility for Navigable*String, but for CData* it lets you
get the string without the CData wrapper."""
if attr == 'string':
return self
else:
raise AttributeError, "'%s' object has no attribute '%s'" % (self.__class__.__name__, attr)
def __unicode__(self):
return str(self).decode(DEFAULT_OUTPUT_ENCODING)
def __str__(self, encodingÞFAULT_OUTPUT_ENCODING):
if encoding:
return self.encode(encoding)
else:
return self


Did I make any mistake?
How can I do this correctly?



2011-05-06



Kerry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110506/9ab0a410/attachment.html>

Search Discussions

  • James Mills at May 6, 2011 at 1:46 am

    On Fri, May 6, 2011 at 11:37 AM, 1011_wxy wrote:
    I got a import error when I use Python 3.2 to import BeautifulSoup 3.2.0 .
    Is there any differences between Python 3.2 and other version? This is my
    first time to use Python3.2 .
    And the error message will be as below.
    Judging from your tracebacks, it would appear
    that BeuituflSoup has not yet been ported to
    and/or fixed for Python 3 compatibility.

    Maybe there's a later version of BeauitifulSOup
    or a development version that does support Python 3?

    There are several Syntax differences between
    Python 2.x and Python 3.x - notably:

    except Exception, e: # Python 2.x

    except Exception as e: # Python 3.x

    I might suggest you take a look at using lxml instead
    which ships with the standard library.

    [ ... ]

    cheers
    James

    --
    -- James Mills
    --
    -- "Problems are solved by method"
  • nirinA raseliarison at May 6, 2011 at 12:27 pm
    [1011_wxy]
    I got a import error when I use Python 3.2 to import BeautifulSoup 3.2.0
    the error i see is a SyntaxError.
    Is there any differences between Python 3.2 and other version?
    yes. and there is a tool, 2to3, which converts
    Python 2.x scripts to work with 3.x.
    And the error message will be as below.

    from BeautifulSoup import BeautifulSoup
    Traceback (most recent call last):
    File "<pyshell#2>", line 1, in <module>
    from BeautifulSoup import BeautifulSoup
    File "C:\Python32\BeautifulSoup.py", line 448
    raise AttributeError, "'%s' object has no attribute '%s'" %
    (self.__class__.__name__, attr)
    ^
    SyntaxError: invalid syntax
    a quick fix here, change:

    raise AttributeError, "'%s' object has no attribute '%s'"
    %(self.__class__.__name__, attr)

    to:

    raise AttributeError("'%s' object has no attribute '%s'"
    %(self.__class__.__name__, attr))

    enclose the string message with parentheses.
    at least, you will not get the SyntaxError.
    but a better solution is to use 2to3.

    i don't use BeautifulSoup, and i'm still stick with
    Python2.5.4 on windows, so can't help much.

    with Linux, it is just:

    2to3 a_python_file_or_a_dir_package

    on windows, i don't know, maybe:

    import subprocess
    subprocess.Popen(('python.exe', 'Tools/Scripts/2to3.py', 'the_package'))

    --
    nirinA

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 6, '11 at 1:37a
activeMay 6, '11 at 12:27p
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase