[python爬虫]获取html中文乱码时的方法
2015-08-02 20:15
801 查看
用python做网页爬虫时经常会出现乱码问题。下面给出解决中文乱码问题的解决方法。
其中需要一个chardet的模块。使用pip install chardet安装即可。pip list命令可以查看安装的包,如果出现有chardet就说明安装好了。
其中需要一个chardet的模块。使用pip install chardet安装即可。pip list命令可以查看安装的包,如果出现有chardet就说明安装好了。
import urllib2 import sys import chardet req = urllib2.Request("http://www.163.com/") content = urllib2.urlopen(req).read() typeEncode = sys.getfilesystemencoding()##系统默认编码 infoencode = chardet.detect(content).get('encoding','utf-8')##通过第3方模块来自动提取网页的编码 html = content.decode(infoencode,'ignore').encode(typeEncode)##先转换成unicode编码,然后转换系统编码输出 print html
相关文章推荐
- [2015-08-02] python013
- python reportlab 生成pdf (二) SimpleDocTemplate
- [2015-08-01] python012
- python reportlab 生成pdf (一)
- python抓取新浪微博配图
- python中time函数用法
- wxPython中按钮、文本控件的简单运用
- Python对列表中字典元素排序
- [python 笔记2]列表、字符串、字典(list?string?map?)
- Kmeans聚类Python程序
- 下载图片-使用beautifulSoup模块
- 用Python实现牛顿法求极值
- machine learning in coding(python):根据关键字合并多个表(构建组合feature)
- python_学习笔记0802
- python科学计算_numpy_ndarray
- python3.4 GUI
- 【Python】循环设计
- python抓包解包
- 用python加cPAMIE加pyinstaller为我柱哥点赞
- wxpython 32 位 ,python 64 位问题