概述
在爬虫爬取网页内容遇到的bug
Debug错误提示:UnicodeEncodeError: 'gbk' codec can't encode character u'xa0' in position 0: illegal multibyte sequence
代码:
response1=urllib2.urlopen(url) r_doc=response1.read() soup=BeautifulSoup(r_doc, 'html.parser',from_encoding='utf-8') content=soup.find('div',id="content") doc=content.get_text() print doc
后来经过验证
print u"中文"
不会报错
但是
print u"xa0中文"
debug产生错误
xa0 属于 latin1 (ISO/IEC_8859-1)中的扩展字符集字符,代表空白符nbsp(non-breaking space)。
latin1 字符集向下兼容 ASCII ( 0x20~0x7e )。通常我们见到的字符多数是 latin1 的,比如在 MySQL 数据库中。经过测验 将代码修改为
使用relace 将爬取内容中的xa0修改为空格,程序成功运行response1=urllib2.urlopen(url) r_doc=response1.read() soup=BeautifulSoup(r_doc, 'html.parser',from_encoding='utf-8') content=soup.find('div',id="content") doc=content.get_text().replace(u"xa0"," ") print doc
最后
以上就是哭泣抽屉为你收集整理的python print unicode错误bug的全部内容,希望文章能够帮你解决python print unicode错误bug所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复