pythonbeautifulsoupsgmlsgmlreader

UnicodeEncodeError in Beautiful soup (python 2.7.1)


I am using Beautiful Soup 3.2 on python 2.7.1 here.

I have recently been trying to get something simple to work, but it seems rather tricky:

I do the following:

temp=BeautifulSoup(urllib2.urlopen(urlList[1], None,15))

However, I get the error:

File "/home/foo/k/kat/BeautifulSoup.py", line 1519, in __init__
BeautifulStoneSoup.__init__(self, *args, **kwargs)
File "/home/foo/k/kat/BeautifulSoup.py", line 1144, in __init__
self._feed(isHTML=isHTML)
File "/home/foo/k/kat/BeautifulSoup.py", line 1186, in _feed
SGMLParser.feed(self, markup)
File "/usr/lib/python2.7/sgmllib.py", line 104, in feed
self.goahead(0)
File "/usr/lib/python2.7/sgmllib.py", line 143, in goahead
k = self.parse_endtag(i)
File "/usr/lib/python2.7/sgmllib.py", line 320, in parse_endtag
self.finish_endtag(tag)
File "/usr/lib/python2.7/sgmllib.py", line 358, in finish_endtag
method = getattr(self, 'end_' + tag)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfa' in
position 4: ordinal not in range(128)

If I run the same loop another time, sometimes, I also get:

File "/home/foo/k/kat/BeautifulSoup.py", line 1519, in __init__
BeautifulStoneSoup.__init__(self, *args, **kwargs)
File "/home/foo/k/kat/BeautifulSoup.py", line 1144, in
__init__
self._feed(isHTML=isHTML)
File "/home/foo/k/kat/BeautifulSoup.py", line 1186, in _feed
SGMLParser.feed(self, markup)
File "/usr/lib/python2.7/sgmllib.py", line 104, in feed
self.goahead(0)
File "/usr/lib/python2.7/sgmllib.py", line 143, in goahead
k = self.parse_endtag(i)
File "/usr/lib/python2.7/sgmllib.py", line 320, in parse_endtag
self.finish_endtag(tag)
File "/usr/lib/python2.7/sgmllib.py", line 358, in finish_endtag
method = getattr(self, 'end_' + tag)
UnicodeEncodeError: 'ascii' codec can't encode characters in position
4-5: ordinal not in range(128)

How do I avoid this errors? Obviously something is wrong with the sgmllib.py.

I tried some solutions from SOF:

*] Tried soup = BeautifulSoup(page, fromEncoding=<encoding of the page>) Result: Dosent work, same errors.

*] Tried upgrading my sgmllib.py from a 2.7.2 version onto my 2.7.1 verision Result: Dosent work, same errors.

*] Tried html = BeautifulSoup(page.encode('utf-8')) Result: Dosent work, same errors.

I would appreciate any suggestions as to how to solve this encode error.


Solution

  • try this code in your module

    if __name__ == "__main__":
        reload(sys)
        sys.setdefaultencoding("utf-8")