2017年03月11日自动化检测函数供检测Url
优采云 发布时间: 2021-05-17 00:00
2017年03月11日自动化检测函数供检测Url
python获取网页编码方法的实现代码
更新时间:2017年3月11日16:26:00贡献:lqh
本文文章主要介绍python的相关信息,以获取实现代码的网页编码方法,有需要的朋友可以参考以下内容
python获取网页编码方法的实现代码
python开发,自动化获取网页编码方式用到了chardet库,字符集检测,这个类在python2.7中没有,需要在官网上下载。
这里我下载好了chardet-2.3.0.tar.gz压缩包文件,只需要将压缩包文件解压后的chardet文件放到python安装包下的
python27/lib/site-packages/下,就可以了。
然后导入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方法,返回字典,然后取出编码方法进行编码
感谢您的阅读,希望对大家有所帮助,感谢您对本网站的支持!