prettify()使用Python 2.7的错误 [英] Prettify() error using python 2.7

查看:679
本文介绍了prettify()使用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())

错误:

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]

我似乎无法得到错误。我使用Python 2.7.9。

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

推荐答案

如果你有一个控制台为ASCII然后打印过程中,有一个从单向code转换为ASCII码,如果有超出ASCII范围字符 - 异常。

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.

但是,如果控制台可以接受单code,那么一切都是正确displayed.Try再次命令和运行程序

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

出口LANG =的en_US.UTF-8

export LANG=en_US.UTF-8

这篇关于prettify()使用Python 2.7的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆