Beautifulsoup 中的“ascii"编解码器错误 [英] 'ascii' codec error in beautifulsoup

查看:20
本文介绍了Beautifulsoup 中的“ascii"编解码器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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)

我正在使用代码:

import urllib2
from BeautifulSoup import BeautifulSoup

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

这是给我的错误.

推荐答案

一个疯狂的猜测:

试试指定页面的编码?

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

这也可能是 Python 安装的问题.如果您在没有 BeautifulSoup 的情况下打印非 ASCII 字符,您是否面临同样的问题?如果是,则需要设置编码:

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"编解码器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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