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/40187913
http://www.coder4.com/archives/4382