
import redis
import json
class RedisCache(object):
"""
使用redis进行爬虫结果的缓存,并可以进行增量爬取
"""
def __init__(self):
self.r = redis.StrictRedis(host="127.0.0.1", port=6379, db=0)
def __setitem__(self, key, value):
"""
将一条解析过得网页数据存入redis
:param key: 带存储数据的地址
:param value: 需要存储的字典数据
:return:
"""
value_str = json.dumps(value)
# redis不存在相应的网址,则将内容存入
if not self.r.exists(key):
self.r.set(key, value_str)
def __getitem__(self, item):
return self.r.get(item)
def __contains__(self, item):
return self.r.exists(item)
#
# rc = RedisCache()
# print(rc['/article/121222988'])