Prettify() error using python 2.7

。_饼干妹妹 提交于 2019-12-13 03:43:19

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/27818815/prettify-error-using-python-2-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!