Tom,
I don't have a great understanding of how Beautiful Soup handles
encodings, but I'll give it a shot. The system for serialization in
Beautiful Soup 3.0.x is different than in 3.1.x. Specifically, the
behavior of __repr__ is slightly different. __repr__ is the hook used
by the interactive interpreter to implicitly print out the results of
some expressions to the terminal.
In BS 3.0.x, __repr__ is identical to __str__. In BS 3.1.x, the
behavior of __repr__ when it comes to encoding is just oh so slightly
different than __str__. The specific code starts at line 615 in
BeautifulSoup.py, version 3.1.0.1. For your code, the difference
apparently will cause a problem with __repr__, but not __str__. The
problem comes when unicode.encode is called with 'utf-8' (the default
for __repr__) instead of None (used by __str__).
unicode.encode('utf-8') chocks on the Euro symbol, but
unicode.encode(None) does not.
The reason that you're running into trouble with "print
soup.findAll('body')" as opposed to "print soup.find('body')" is
caused by how print interacts with list.__str__. From what I can tell,
list.__str__ goes through its contents and uses the C equivalent to
repr(...) on them. If you were to manual loop through the list and
call str(...) on the individual resulting elements you wouldn't get
any UnicodeEncodeErrors. "print soup.find('body')" directly calls
__str__.
As an aside, if you ever need to print out something as a list you can
use a list comprehension to explicitly run __unicode__.
[unicode(element) for element in soup.findAll('tag-name')]
Cheers!
Aaron DeVore
> --
>
> You received this message because you are subscribed to the Google Groups "beautifulsoup" group.
> To post to this group, send email to
beauti...@googlegroups.com.
> To unsubscribe from this group, send email to
beautifulsou...@googlegroups.com.
> For more options, visit this group at
http://groups.google.com/group/beautifulsoup?hl=.
>
>
>