店播爬取Python脚本

proxy.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import json
  2. import time
  3. import sys
  4. from random import choice
  5. # from libs.db_redis import DbRedis
  6. from log.print_log import PrintLog
  7. sys.path.append("..")
  8. from rds_model.db_redis import DbRedis
  9. class Proxy:
  10. redis = DbRedis.connect()
  11. proxy_info = ''
  12. @staticmethod
  13. def get():
  14. while True:
  15. proxy = Proxy.random_proxy()
  16. if proxy is not None:
  17. return proxy
  18. PrintLog.print('未拿到有效的代理')
  19. time.sleep(0.1)
  20. @staticmethod
  21. def random_proxy():
  22. key = 'IpProxyHash'
  23. proxy_dict = Proxy.redis.hgetall(key)
  24. if (proxy_dict is None) or (len(proxy_dict) == 0):
  25. return
  26. proxy_list = list(proxy_dict)
  27. now = int(time.time())
  28. while True:
  29. proxy = choice(proxy_list)#Proxy.choice_proxy()
  30. if proxy is None:
  31. return
  32. proxy_info = proxy_dict.get(proxy)
  33. if proxy_info is None:
  34. continue
  35. Proxy.proxy_info = proxy_info
  36. proxy_info = json.loads(proxy_info)
  37. expire_at = int(proxy_info.get('expired_at'))
  38. # 删除过期的代理
  39. if expire_at <= now:
  40. Proxy.redis.hdel(key, proxy)
  41. proxy_list.remove(proxy)
  42. continue
  43. return proxy
  44. @staticmethod
  45. def del_proxy(proxy):
  46. proxy_info = Proxy.redis.hget('IpProxyHash', proxy)
  47. if proxy_info is None:
  48. return
  49. proxy_info = json.loads(proxy_info)
  50. min_expired_at = proxy_info.get('min_expired_at')
  51. if min_expired_at is None:
  52. min_expired_at = 0
  53. min_expired_at = int(min_expired_at)
  54. now = int(time.time())
  55. if min_expired_at >= now:
  56. return
  57. # 删除失效的代理
  58. print('删除失效代理:' + proxy)
  59. @staticmethod
  60. def dailiyun_get():
  61. while True:
  62. proxy = Proxy.dailiyun_random_proxy()
  63. if proxy is not None:
  64. return proxy
  65. PrintLog.print('未拿到有效的代理')
  66. time.sleep(0.1)
  67. @staticmethod
  68. def dailiyun_random_proxy():
  69. key = 'IpProxyHashDaiLiYun'
  70. proxy_dict = Proxy.redis.hgetall(key)
  71. if (proxy_dict is None) or (len(proxy_dict) == 0):
  72. return
  73. proxy_list = list(proxy_dict)
  74. now = int(time.time())
  75. while True:
  76. proxy = choice(proxy_list)#Proxy.choice_proxy()
  77. if proxy is None:
  78. return
  79. proxy_info = proxy_dict.get(proxy)
  80. if proxy_info is None:
  81. continue
  82. Proxy.proxy_info = proxy_info
  83. proxy_info = json.loads(proxy_info)
  84. expire_at = int(proxy_info.get('expired_at'))
  85. # 删除过期的代理
  86. if expire_at <= now:
  87. Proxy.redis.hdel(key, proxy)
  88. proxy_list.remove(proxy)
  89. continue
  90. return proxy
  91. @staticmethod
  92. def dailiyun_del_proxy(proxy):
  93. proxy_info = Proxy.redis.hget('IpProxyHashDaiLiYun', proxy)
  94. if proxy_info is None:
  95. return
  96. proxy_info = json.loads(proxy_info)
  97. min_expired_at = proxy_info.get('min_expired_at')
  98. if min_expired_at is None:
  99. min_expired_at = 0
  100. min_expired_at = int(min_expired_at)
  101. now = int(time.time())
  102. if min_expired_at >= now:
  103. return
  104. # 删除失效的代理
  105. print('删除失效代理:' + proxy)
  106. @staticmethod
  107. def rola_get():
  108. while True:
  109. proxy = Proxy.rola_random_proxy()
  110. if proxy is not None:
  111. return proxy
  112. PrintLog.print('未拿到有效的代理')
  113. time.sleep(0.1)
  114. @staticmethod
  115. def rola_random_proxy():
  116. key = 'IpProxyHashRola'
  117. proxy_dict = Proxy.redis.hgetall(key)
  118. if (proxy_dict is None) or (len(proxy_dict) == 0):
  119. return
  120. proxy_list = list(proxy_dict)
  121. now = int(time.time())
  122. while True:
  123. proxy = choice(proxy_list)#Proxy.choice_proxy()
  124. if proxy is None:
  125. return
  126. proxy_info = proxy_dict.get(proxy)
  127. if proxy_info is None:
  128. continue
  129. Proxy.proxy_info = proxy_info
  130. proxy_info = json.loads(proxy_info)
  131. expire_at = int(proxy_info.get('expired_at'))
  132. # 删除过期的代理
  133. if expire_at <= now:
  134. Proxy.redis.hdel(key, proxy)
  135. proxy_list.remove(proxy)
  136. continue
  137. return proxy
  138. @staticmethod
  139. def rola_del_proxy(proxy):
  140. proxy_info = Proxy.redis.hget('IpProxyHashRola', proxy)
  141. if proxy_info is None:
  142. return
  143. proxy_info = json.loads(proxy_info)
  144. min_expired_at = proxy_info.get('min_expired_at')
  145. if min_expired_at is None:
  146. min_expired_at = 0
  147. min_expired_at = int(min_expired_at)
  148. now = int(time.time())
  149. if min_expired_at >= now:
  150. return
  151. # 删除失效的代理
  152. print('删除失效代理:' + proxy)