python 获取网页编码格式

f = urllib2.urlopen(url,timeout=10)
data = f.read()   
# decode the html
contentType = f.headers.get('Content-Type')
if contentType.find("gbk"):
    data = unicode(data, "GBK").encode("utf-8")
elif contentType.find("utf-8"):
    pass

原文地址:https://www.cnblogs.com/shijiaoyun/p/4469470.html