运行环境

  • Python 3.6
  • requests==2.18.4

chardet模块

In[2]: import requests
In[3]: resp = requests.get('http://baidu.com')
In[4]: resp
Out[4]: <Response [200]>
In[5]: resp.encoding
Out[5]: 'ISO-8859-1'

requests模块

In[2]: import chardet
In[3]: from urllib.request import urlopen
In[4]: url = 'http://www.baidu.com'
In[5]: html = urlopen(url).read()
In[6]: print(chardet.detect(html))
{'encoding': 'utf-8', 'confidence': 0.99, 'language': ''}