Python中安装编码检测工具chardet

在Github建好博客后开始学习新的一章,爬虫,首先需要安装一个检测网页代码编码的工具chardet. 参考chardet官网

1. pip 安装

在cmd输入命令

1
pip install chardet

2. 使用detect()模块检测

在IDLE

1
2
3
4
5
>>> import urllib.request as ur
>>> respond = ur.urlopen('https://sonichuang.github.io')
>>> import chardet
>>> chardet.detect(respond.read())
{'encoding': 'utf-8', 'confidence': 0.99, 'language': ''}

意思百分之九十九是utf-8, 还可以检测字符串的语言,如:中文,英文等