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'} #假裝移動瀏覽器 #header = {'User-Agent':'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'} req = urllib2.Request(url=url,headers=headers) data = urllib2.urlopen(req).read() #若已知response頁面的charset爲UTF-8,先對其解碼成unicode,再按照type指定的字符集從新編碼 print data.decode("UTF-8").encode(type) #或者
print unicode(data,'utf-8').encode('gb2312')
url含有中文,進行編碼:瀏覽器
import urllib2 url = u"http://www.baidu.com/wd=測試" urllib2.urlopen(url.encode('utf-8')).read()
引自:測試
http://blog.csdn.net/xyw_blog/article/details/40187913ui
http://www.coder4.com/archives/4382編碼