关于Python 中unicode 转码的问题
2014-10-08 13:40
232 查看
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 进行赋值
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 进行赋值
相关文章推荐
- 关于Python3-Win控制台程序: UnicodeEncodeError: gbk can't encode xxx的问题
- 关于python的str和unicode以及编码的问题
- 关于python中json load出来编码为unicode的问题的解决
- 关于python中json load出来编码为unicode的问题的解决
- 关于python2.7的md5加密遇到的问题(TypeError: Unicode-objects must be encoded before hashing)
- Python转码的问题
- Python转码问题的解决方法
- 关于混排文本修改转换系列问题--ASCII & UniCode
- 一个关于Unicode字符编码的奇怪问题
- 请教各位老师关于:Python 类和对象创建撤销问题 (Python2.5.2,linux)
- 关于Script.NET Python开发包的一些问题解答
- 一个关于Unicode字符编码的奇怪问题
- 关于UNICODE字符串的匹配问题
- 关于CTime::Format在Unicode下的输出问题及解决办法
- python unicode ascii编码在windows,*nix上的问题2010-01-05
- Python转码问题的解决方法
- 关于中文字符UNICODE编码的三个问题(基于此可以求中文字符的笔划和首拼)
- 一个关于Python的类覆盖的问题
- 一个关于Unicode字符编码的奇怪问题
- 在论坛中找到的几个关于Python的小问题的解决方法