您的位置:首页 > 编程语言 > Python开发

Python获取网页编码的两种方法——requests、chardet

2017-12-12 16:57 417 查看
运行环境:
Python3.6
requests2.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': ''}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: