可扩展的Cookies池,用无头浏览器登录并生成cookie供给爬虫使用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.9 KiB

8 years ago
import random
import redis
from cookiespool.config import *
class RedisClient(object):
7 years ago
def __init__(self, type, website, host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWORD):
8 years ago
"""
初始化Redis连接
:param host: 地址
:param port: 端口
:param password: 密码
"""
7 years ago
self.db = redis.Redis(host=host, port=port, password=password, decode_responses=True)
self.type = type
self.website = website
8 years ago
7 years ago
def key(self):
8 years ago
"""
7 years ago
得到格式化的username
:param username: 最后一个参数username
8 years ago
:return:
"""
7 years ago
return "{type}:{website}".format(type=self.type, website=self.website)
8 years ago
7 years ago
def set(self, username, value):
8 years ago
"""
设置键值对
7 years ago
:param username:
8 years ago
:param value:
:return:
"""
7 years ago
return self.db.hset(self.key(), username, value)
8 years ago
7 years ago
def get(self, username):
8 years ago
"""
根据键名获取键值
7 years ago
:param username:
8 years ago
:return:
"""
7 years ago
return self.db.hget(self.key(), username)
8 years ago
7 years ago
def delete(self, username):
8 years ago
"""
根据键名删除键值对
7 years ago
:param username:
8 years ago
:return:
"""
7 years ago
return self.db.hdel(self.key(), username)
8 years ago
7 years ago
def count(self):
8 years ago
"""
7 years ago
获取数目
:return: 数目
8 years ago
"""
7 years ago
return self.db.hlen(self.key())
8 years ago
def random(self):
"""
7 years ago
随机得到键值
8 years ago
:return:
"""
7 years ago
return random.choice(self.db.hvals(self.key()))
8 years ago
7 years ago
def usernames(self):
8 years ago
"""
7 years ago
获取所有账户信息
8 years ago
:return:
"""
7 years ago
return self.db.hkeys(self.key())
8 years ago
def all(self):
"""
7 years ago
获取所有键值对
8 years ago
:return:
"""
7 years ago
return self.db.hgetall(self.key())
8 years ago
if __name__ == '__main__':
7 years ago
conn = RedisClient('accounts', 'weibo')
result = conn.set('hell2o', 'sss3s')
print(result)