python 获取网页编码方式实现代码,python编码
python 获取网页编码方式实现代码,python编码
python 获取网页编码方式实现代码
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> python开发,自动化获取网页编码方式用到了chardet库,字符集检测,这个类在python2.7中没有,需要在官网上下载。 这里我下载好了chardet-2.3.0.tar.gz压缩包文件,只需要将压缩包文件解压后的chardet文件放到python安装包下的 python27/lib/site-packages/下,就可以了。</span>
然后import chardet
下面写了一个自动化检测的函数供检测Url连接,然后返回网页url的编码方式。
import chardet #字符集检测 import urllib url="http://www.jd.com" def automatic_detect(url): content=urllib.urlopen(url).read() result=chardet.detect(content) encoding=result['encoding'] return encoding urls=['http://www.baidu.com','http://www.163.com','http://dangdang.com'] for url in urls: print url,automatic_detect(url)
上面用到了chardet类的detect方法,返回字典,然后取出编码方式encoding
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关内容
- python函数的5种参数详解,python5种参数详解
- 浅析python中SQLAlchemy排序的一个坑,pythonsqlalchemy
- Python数据结构之翻转链表,python数据结构翻转
- Python 数据结构之旋转链表,python数据结构
- 利用python程序生成word和PDF文档的方法,
- python用装饰器自动注册Tornado路由详解,pythontornado
- python 出现SyntaxError: non-keyword arg after keyword arg错误解决
- 深入理解Python对Json的解析,深入理解pythonjson
- 关于Python面向对象编程的知识点总结,python面向对象编
- Python变量和数据类型详解,python变量数据类型
评论关闭