【发布时间】:2015-01-29 12:40:25
【问题描述】:
我遇到了 BeautifulSoup 的编码问题。在我的开发环境中一切正常(Ubuntu、Python 3.4、Django 开发服务器)。在生产服务器上(Ubuntu,Python 3.4,相同版本的 Django 和 BeautifulSoup - 唯一的区别是使用 gunicorn 和 Nginx)我得到:
'ascii' codec can't decode byte 0xc3 in position 301: ordinal not in range(128)
trackback 显示问题出在“BeautifulSoup(data)”语句中。
data = open(os.path.join(BASE_DIR, 'data/file.xml'))
data = BeautifulSoup(data)
BeautifulSoup 似乎尝试使用 ascii,但文件是 utf-8 编码的。我尝试了不同的方法,我可以在这里找到堆栈溢出,但它们中的大多数依赖 Python 2 - 但我使用 Python 3。我也尝试过类似的方法:
data = BeautifulSoup(str(data.read()))
但这不起作用。
非常感谢任何帮助。
【问题讨论】:
-
您没有编码错误。您有一个de编码错误。
标签: python django python-3.x beautifulsoup lxml