Python獲取網頁編碼的兩種方法——requests、chardet

運行環境:Python3.6requests2.18.4

方法一:使用requests模塊

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

方法二:使用chardet模塊

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': ''}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章