Python通过HTTP协议定期抓取文件
2007-07-26 18:56
197 查看
可以扩充成为简单的抓取工具,定时抓取
#!usr/bin/python
import urllib2,time;
class ErrorHandler(urllib2.HTTPDefaultErrorHandler):
def http_error_default(self, req, fp, code, msg, headers):
result = urllib2.HTTPError(req.get_full_url(), code, msg, headers, fp)
result.status = code
return result
URL='http://www.ibm.com/developerworks/js/ajax1.js'
req=urllib2.Request(URL)
mgr=urllib2.build_opener(ErrorHandler())
while True:
ns=mgr.open(req)
if(ns.headers.has_key('last-modified')):
modified=ns.headers.get('last-modified')
if(ns.code==304):
print '''
==============================
NOT MODIFIED
==============================
'''
elif(ns.code==200):
print ns.read()
else:
print 'there is an error';
if(not locals().has_key('modified')):
modified=time.time();
req.add_header('If-Modified-Since',modified)
time.sleep(10)
#!usr/bin/python
import urllib2,time;
class ErrorHandler(urllib2.HTTPDefaultErrorHandler):
def http_error_default(self, req, fp, code, msg, headers):
result = urllib2.HTTPError(req.get_full_url(), code, msg, headers, fp)
result.status = code
return result
URL='http://www.ibm.com/developerworks/js/ajax1.js'
req=urllib2.Request(URL)
mgr=urllib2.build_opener(ErrorHandler())
while True:
ns=mgr.open(req)
if(ns.headers.has_key('last-modified')):
modified=ns.headers.get('last-modified')
if(ns.code==304):
print '''
==============================
NOT MODIFIED
==============================
'''
elif(ns.code==200):
print ns.read()
else:
print 'there is an error';
if(not locals().has_key('modified')):
modified=time.time();
req.add_header('If-Modified-Since',modified)
time.sleep(10)
相关文章推荐
- Python通过HTTP协议定期抓取文件
- Python通过HTTP协议定期抓取网页
- 通过解析HTTP协议自己实现文件上传
- python通过wxPython打开一个音频文件并播放的方法
- 用python通过ftp将文件上传到服务器
- 通过deb包安装Python库时怎么生成pyc文件?
- python 通过pip生成requirements.txt,以及通过该文件导入依赖库
- python(.py)文件在windows系统中通过命令行执行
- anaconda 通过命令行执行 python 文件
- 统计元素个数+字典存入文件(通过序列化)的python实现
- python 通过文件路径获取文件hash值
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- 重构前的程序:通过rsync命令抓取日志文件
- python3 - 通过BeautifulSoup 4抓取百度百科人物相关链接
- Python通过代理多线程抓取图片
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- 通过python的paramiko抓取多台服务器信息,并通过html格式发送邮件到群组
- Android--之通过Http协议上传文件
- 通过python顺序修改文件名字的方法
- 在使用python requests 库时,通过post上传名称为中文文件时我遇到的问题