Python问题总结

1.Python抓取网页内容显示中文乱码

解决:

 1 #coding=utf-8
 2 
 3 import urllib2
 4 import sys
 5 
 6 page = 1
 7 url = 'http://www.qiushibaike.com/hot/page/' + str(page)
 8 user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
 9 headers = { 'User-Agent' : user_agent}
10 
11 
12 
13 try:
14     request = urllib2.Request(url, headers = headers)
15     response = urllib2.urlopen(request)
16     type = sys.getfilesystemencoding()   #关键
17     #print type
18     print response.read().decode("UTF-8").encode(type)   #关键
19     #print response.read()
20 
21 except urllib2.URLError, e:
22     if hasattr(e, "code"):
23         print e.code
24     if hasattr(e, "reason"):
25         print e.reason

 

posted @ 2016-09-27 12:18  lbj_myboy  阅读(135)  评论(0编辑  收藏  举报