3
3
from proxypool .setting import REDIS_HOST , REDIS_PORT , REDIS_PASSWORD , REDIS_KEY
4
4
from proxypool .setting import MAX_SCORE , MIN_SCORE , INITIAL_SCORE
5
5
from random import choice
6
+ import re
6
7
7
8
8
9
class RedisClient (object ):
@@ -22,6 +23,9 @@ def add(self, proxy, score=INITIAL_SCORE):
22
23
:param score: 分数
23
24
:return: 添加结果
24
25
"""
26
+ if not re .match ('\d+\.\d+\.\d+\.\d+\:\d+' , proxy ):
27
+ print ('代理不符合规范' , proxy , '丢弃' )
28
+ return
25
29
if not self .db .zscore (REDIS_KEY , proxy ):
26
30
return self .db .zadd (REDIS_KEY , score , proxy )
27
31
@@ -84,14 +88,18 @@ def all(self):
84
88
:return: 全部代理列表
85
89
"""
86
90
return self .db .zrangebyscore (REDIS_KEY , MIN_SCORE , MAX_SCORE )
91
+
92
+ def batch (self , start , stop ):
93
+ """
94
+ 批量获取
95
+ :param start: 开始索引
96
+ :param stop: 结束索引
97
+ :return: 代理列表
98
+ """
99
+ return self .db .zrevrange (REDIS_KEY , start , stop - 1 )
87
100
88
101
89
102
if __name__ == '__main__' :
90
103
conn = RedisClient ()
91
- result = conn .all ( )
104
+ result = conn .batch ( 680 , 688 )
92
105
print (result )
93
- random = conn .random ()
94
- print ('Random' , random )
95
- top = conn .top ()
96
- print ('Top' , top )
97
- conn .decrease ('a' )
0 commit comments