Abhishek Bhatia
Abhishek Bhatia

Reputation: 9796

Prettify() error using python 2.7

Code:

import urllib2
from bs4 import BeautifulSoup

page1 = urllib2.urlopen("http://en.wikipedia.org/wiki/List_of_human_stampedes")
soup = BeautifulSoup(page1)

print(soup.prettify())

Error:

Traceback (most recent call last):
  File "C:\Users\sony\Desktop\Trash\Crawler Try\try2.py", line 7, in <module>
    print(soup.prettify())
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 8775: ordinal not in range(128)
[Finished in 2.4s with exit code 1]

I can't seem to get the error. I am using Python 2.7.9.

Upvotes: 0

Views: 884

Answers (1)

user3868300
user3868300

Reputation:

If you have a console as ASCII then during print, there is a conversion from unicode to ascii, and if there is character outside ASCII scope - exception is thrown.

But if console can accept unicode, then everything is correctly displayed.Try this command and run program again

export LANG=en_US.UTF-8

Upvotes: 1

Related Questions