码迷,mamicode.com
首页 > 编程语言 > 详细

python解决urllib2乱码问题

时间:2014-10-17 16:54:14      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:python   urllib2   乱码   

举例:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib 
import urllib2
  
def main():
	url = "http://www.douban.com"
	#浏览器头
	headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
	req = urllib2.Request(url=url,headers=headers)
	data = urllib2.urlopen(req).read()
	print data
	return 0
    
if __name__ == '__main__':
	main()

打印出来的内容中,汉字为乱码。解决方法:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib 
import urllib2
import sys 
type = sys.getfilesystemencoding()
    
def main():
	url = "http://www.douban.com"
	#浏览器头
	headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
	req = urllib2.Request(url=url,headers=headers)
	data = urllib2.urlopen(req).read()
	print data.decode("UTF-8").encode(type)
	return 0
    
if __name__ == '__main__':
	main()

有关python的encode和decode用法参见:http://blog.csdn.net/xyw_blog/article/details/40188037


本文为xyw_Eliot原创,转载请注明出处:http://blog.csdn.net/xyw_blog/article/details/40187913

python解决urllib2乱码问题

标签:python   urllib2   乱码   

原文地址:http://blog.csdn.net/xyw_blog/article/details/40187913

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!