python 获取网页编码方式实现代码
python获取网页编码方式实现代码
python开发,自动化获取网页编码方式用到了chardet库,字符集检测,这个类在python2.7中没有,需要在官网上下载。 这里我下载好了chardet-2.3.0.tar.gz压缩包文件,只需要将压缩包文件解压后的chardet文件放到python安装包下的 python27/lib/site-packages/下,就可以了。
然后importchardet
下面写了一个自动化检测的函数供检测Url连接,然后返回网页url的编码方式。
importchardet#字符集检测 importurllib url="http://www.jd.com" defautomatic_detect(url): content=urllib.urlopen(url).read() result=chardet.detect(content) encoding=result['encoding'] returnencoding urls=['http://www.baidu.com','http://www.163.com','http://dangdang.com'] forurlinurls: printurl,automatic_detect(url)
上面用到了chardet类的detect方法,返回字典,然后取出编码方式encoding
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下