抓取jsp网页源代码( 自动化检测函数供检测Url连接连接)

优采云 发布时间: 2022-02-04 00:16

  抓取jsp网页源代码(

自动化检测函数供检测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方法,返回一个字典,然后取出编码方法

  感谢您的阅读,希望对您有所帮助,感谢您对本站的支持!

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线