python解决urllib2乱码问题
2014-10-17 16:27
246 查看
举例:
打印出来的内容中,汉字为乱码。解决方法:
有关python的encode和decode用法参见:http://blog.csdn.net/xyw_blog/article/details/40188037
本文为xyw_Eliot原创,转载请注明出处:http://blog.csdn.net/xyw_blog/article/details/40187913
#!/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乱码问题
- Python字符串的encode与decode研究心得——解决乱码问题
- python邮件发送(带附件,解决中文乱码问题)
- python BeautifulSoup解决中文乱码问题
- Python解决乱码问题
- Python字符串的encode与decode研究心得乱码问题解决方法
- (转)Python字符串的encode与decode研究心得——解决乱码问题
- Python字符串的encode与decode研究心得乱码问题解决方法
- 基于python发送邮件的乱码问题的解决办法
- Python字符串的encode与decode研究心得乱码问题解决方法
- Python字符串的encode与decode研究心得乱码问题解决方法
- 安装python-mutagen 批量修改mp3字符编码格式,解决Amarok播放器乱码问题
- 基于python发送邮件的乱码问题的解决办法
- Python字符串的encode与decode研究心得——解决乱码问题
- Python字符串的encode与decode研究心得——解决乱码问题
- Python字符串的encode与decode研究心得乱码问题解决方法
- python字符串的encode与decode研究心得——解决乱码问题
- Python字符串的encode与decode研究心得乱码问题解决方法
- Python字符串的encode与decode研究心得乱码问题解决方法