UnicodeEncodeError with BeautifulSoup 3.1.0.1 and

2019-04-10 00:30发布

With BeautifulSoup 3.1.0.1 and Python 2.5.2, and trying to parse a web page in French. However, as soon as I call findAll, I get the following error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 1146: ordinal not in range(128)

Below is the code I am currently running:

import urllib2
from BeautifulSoup import BeautifulSoup
page = urllib2.urlopen("http://fr.encarta.msn.com/encyclopedia_761561798/Paris.html")
soup = BeautifulSoup(page, fromEncoding="latin1")
r = soup.findAll("table")
print r

Does anybody have an idea why?

Thanks!

UPDATE: As resquested, below is the full Traceback

Traceback (most recent call last):
  File "[...]\test.py", line 6, in <module>
    print r
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1146-1147: ordinal not in range(128)

1条回答
Anthone
2楼-- · 2019-04-10 00:56

Here is another idea. Your terminal is not capable of displaying an unicode string from Python. The interpreter tries to convert it to ASCII first. You should encode it explicitly before printing. I don't know the exact semantics of soup.findAll(). But it is probably something like:

for t in  soup.findAll("table"):
    print t.encode('latin1')

If t really is a string. Maybe its just another object from which you have to build the data that you want to display.

查看更多
登录 后发表回答