可扩展的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.

83 lines
2.0 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.StrictRedis(host=host, port=port, password=password, decode_responses=True)
7 years ago
self.type = type
self.website = website
8 years ago
7 years ago
def name(self):
8 years ago
"""
7 years ago
获取Hash的名称
:return: Hash名称
8 years ago
"""
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: 用户名
:param value: 密码或Cookies
8 years ago
:return:
"""
7 years ago
return self.db.hset(self.name(), 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.name(), username)
8 years ago
7 years ago
def delete(self, username):
8 years ago
"""
根据键名删除键值对
7 years ago
:param username: 用户名
:return: 删除结果
8 years ago
"""
7 years ago
return self.db.hdel(self.name(), 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.name())
8 years ago
def random(self):
"""
7 years ago
随机得到键值用于随机Cookies获取
:return: 随机Cookies
8 years ago
"""
7 years ago
return random.choice(self.db.hvals(self.name()))
8 years ago
7 years ago
def usernames(self):
8 years ago
"""
7 years ago
获取所有账户信息
7 years ago
:return: 所有用户名
8 years ago
"""
7 years ago
return self.db.hkeys(self.name())
8 years ago
def all(self):
"""
7 years ago
获取所有键值对
7 years ago
:return: 用户名和密码或Cookies的映射表
8 years ago
"""
7 years ago
return self.db.hgetall(self.name())
8 years ago
if __name__ == '__main__':
7 years ago
conn = RedisClient('accounts', 'weibo')
result = conn.set('hell2o', 'sss3s')
print(result)