python操作redis

1.插入数据并查重

'''
redis数据库 插入并查重
props: 详情页url
return: 1(插入成功), 0(插入失败,重复)
'''
def check_rep(page):
    re = redis.Redis(host='192.168.0.229', port=6379, db=3, password='123456')
    ck_flag = re.sadd("url_ck", page)
    print('当前抓取总数: {0}'.format(re.scard("url_ck")))
    return ck_flag

2.删除单条数据

'''
redis数据库 删除单条数据
props: 详情页url
return: None
'''
def remove_one(page):
    re = redis.Redis(host='192.168.0.229', port=6379, db=3, password='123456')
    re.srem("url_ck", page)
    print('抓取失败,本页移除缓存: {0}'.format(re.scard("url_ck")))

3.数据总数查询

'''
redis数据库 数据总数查询
props: None
return: 数据总数
'''
def get_count():
    re = redis.Redis(host='192.168.0.229', port=6379, db=3, password='123456')
    return re.scard("url_ck")

4.键值对 新建或更新

'''
redis数据库 哈希散列 新建或更新键值对
props: 建,值
return: None
'''
def update_cover_num(key, value):
    re = redis.Redis(host='192.168.0.229', port=6379, db=3, password='123456')
    # redis散列键值对
    re.hset("cover_dir", str(key), str(value))

5.获取全部键值对

'''
redis数据库 哈希散列 获取全部键值对
props: 建,值
return: None
'''
def get_all_cover_num():
    re = redis.Redis(host='192.168.0.229', port=6379, db=3, password='123456')
    # redis散列键值对
    return re.hgetall("cover_dir")

6.获取对应键的值

'''
redis数据库 哈希散列 获取对应建的值
props: 建
return: 值
'''
def get_cover_num(key):
    re = redis.Redis(host='192.168.0.229', port=6379, db=3, password='123456')
    # redis散列键值对
    return re.hmget("cover_dir", str(key))

你可能感兴趣的:(python常用方法,redis,python,数据库)