在beautifulsoup“ASCII”codeC错误 [英] 'ascii' codec error in beautifulsoup

查看:168
本文介绍了在beautifulsoup“ASCII”codeC错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用beautifulsoup从html页面刮数据。直到昨天每一件事情都很好。但现在我得到的错误:

I am using beautifulsoup for scraping data from the html page. Till yesterday every thing was fine. But Now i am getting the error:

'ascii' codec can't encode character u'\xa9' in position 86700: ordinal not in range(128)

我使用的是code:

i am using the code:

import urllib2
from BeautifulSoup import BeautifulSoup

page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page)

这是给我的错误。

推荐答案

一个大胆猜测:

尝试指定页面编码?

soup = BeautifulSoup(page, fromEncoding=<encoding of the page>)

这也可以使用Python安装问题。如果打印非ASCII字符不BeautifulSoup,你面临同样的问题?如果是,那么你需要设置编码:

This can also be a problem with the Python installation. If you print non-ASCII characters without BeautifulSoup, do you face the same problem? If yes, then you need to set the encoding:

import sys
sys.setdefaultencoding("utf-8") # or whatever you want the default encoding to be.

这篇关于在beautifulsoup“ASCII”codeC错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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