python urllib.urlopen获得response后检查http响应头中的content type,,有时候在抓取到网页内容后
python urllib.urlopen获得response后检查http响应头中的content type,,有时候在抓取到网页内容后
有时候在抓取到网页内容后需要检查content-type,下面代码演示如何检查urllib.urlopen方法返回响应的http头
import urllibfrom types import *def iscontenttype(URLorFile,contentType='text'): """ Return true or false (1 or 0) based on HTTP Content-Type. Accepts either a url (string) or a "urllib.urlopen" file. Defaults to 'text' type. Only looks at start of content-type, so you can be as vague or precise as you want. For example, 'image' will match 'image/gif' or 'image/jpg'. """ result = 1 try: if type(URLorFile) == StringType: file=urllib.urlopen(URLorFile) else: file = URLorFile testType=file.info().getheader("Content-Type") if testType and testType.find(contentType) == 0: result=1 else: result=0 if type(URLorFile) == StringType: file.close() return result except: return 0
相关内容
- python转换字符串为datetime类型,pythondatetime,>>>
- Python 图片蜘蛛人,,[Python]代码#c
- Python设置windows进程的优先级,python优先级,下面的方法
- Python使用PIL裁剪贴图代码,pythonpil贴图代码,Python使用
- python通过HTMLParser抓取网页上的全部链接,pythonhtmlpars
- python压缩和读取.tar.bz2格式的压缩包,python.tar.bz2,#压缩
- webpy输出json例子代码,webpy输出json,webpy中可以方便的输
- python的反射:动态获得模块,类,python模块,python的反射
- python使用urllib2抓取网页时的错误处理,pythonurllib2,try
- python计算代码执行时间,python计算代码,import times
评论关闭