首页 > 编程语言 > 详细

关于Python 中unicode 转码的问题

时间:2014-10-08 14:13:15      阅读:361      评论:0      收藏:0      [点我收藏+]

Python 中urllib2.urlopen 中存在中文转码问题,解决方法如下:

1.

import BeautifulSoup
import chardet

response =urllib2.urlopen(‘%s‘%line)
#response.decode(‘utf-8‘)
#response = urllib2.urlopen(‘http://www.baidu.com/‘)
html = response.read()
pdb.set_trace()
#print html.decode(‘big5‘).encode(‘utf8‘)
urlcodestyle=chardet.detect(html)
sourcehtml=html.decode(‘%s‘%urlcodestyle[‘encoding‘]).encode(‘utf-8‘)

2.sourcehtml 的使用方法:

import BeautifulSoup
"""
if ‘encoding‘ in urlcodestyle:
soup=BeautifulSoup(html,fromEncoding="%s"%urlcodestyle[‘encoding‘])
else :
soup=BeautifulSoup(html,fromEncoding="gb18030")
"""

最好能够通过获得请求页面的编码格式,然后再对fromEncoding 进行赋值

关于Python 中unicode 转码的问题

原文:http://www.cnblogs.com/yuyezhulan/p/4010988.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!