【python】解决urllib2乱码问题
来源:互联网 发布:东欧社会主义国家 知乎 编辑:程序博客网 时间:2024/05/17 01:07
在使用python爬取数据的时候,发现获取到的数据在VSCode中,中文乱码:
错误版本如下:
import urllibimport urllib2import systype = sys.getfilesystemencoding()page = 1url = 'http://www.qiushibaike.com/hot/page/' + str(page)user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'headers = { 'User-Agent' : user_agent }try: request = urllib2.Request(url, headers=headers) response = urllib2.urlopen(request) data = response.read() print dataexcept urllib2.URLError, e: if hasattr(e, "code"): print e.code if hasattr(e, "reason"): print e.reson
在网上找了资料以后,终于解决乱码问题:
import urllibimport urllib2import systype = sys.getfilesystemencoding()page = 1url = 'http://www.qiushibaike.com/hot/page/' + str(page)user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'headers = { 'User-Agent' : user_agent }try: request = urllib2.Request(url, headers=headers) response = urllib2.urlopen(request) data = response.read() data = data.decode('UTF-8') data = data.encode(type) print dataexcept urllib2.URLError, e: if hasattr(e, "code"): print e.code if hasattr(e, "reason"): print e.reson
0 0
- python解决urllib2乱码问题
- 【python】解决urllib2乱码问题
- python urllib2 解决重定向问题
- urllib2乱码解决
- python解决乱码问题
- python解决mysql乱码问题
- Python解决中文乱码问题
- 解决python中文乱码问题
- python解决乱码问题-文件
- python解决文件乱码问题
- Python解决中文乱码问题
- Python解决中文乱码问题
- 爬取网页乱码 Python urllib2库
- Python解决插入数据库乱码问题
- python解决linux下unzip乱码问题
- Python解决插入数据库乱码问题
- 解决Windows下python中文乱码问题
- 中文字符乱码问题简单解决 python
- 第十周
- 认识三极管
- DES算法描述
- 二叉树10:最大二叉搜索子树
- access注入及工具使用
- 【python】解决urllib2乱码问题
- K
- JS闭包-02
- 在PhpStorm中使用git将项目上传到码云
- hihocoder 1121 : 二分图一•二分图判定
- Android adb 常用命令
- IE9对CSS3属性的支持情况
- Mac版tomcat下载安装启动
- android中编译linux内核