店播爬取Python脚本

proxy.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import json
  2. import time
  3. from random import choice
  4. from libs.db_redis import DbRedis
  5. from log.print_log import PrintLog
  6. class Proxy:
  7. redis = DbRedis.connect()
  8. proxy_info = ''
  9. @staticmethod
  10. def get():
  11. while True:
  12. proxy = Proxy.random_proxy()
  13. if proxy is not None:
  14. return proxy
  15. PrintLog.print('未拿到有效的代理')
  16. time.sleep(0.1)
  17. @staticmethod
  18. def random_proxy():
  19. key = 'IpProxyHash'
  20. proxy_dict = Proxy.redis.hgetall(key)
  21. if (proxy_dict is None) or (len(proxy_dict) == 0):
  22. return
  23. proxy_list = list(proxy_dict)
  24. now = int(time.time())
  25. while True:
  26. proxy = choice(proxy_list)#Proxy.choice_proxy()
  27. if proxy is None:
  28. return
  29. proxy_info = proxy_dict.get(proxy)
  30. if proxy_info is None:
  31. continue
  32. Proxy.proxy_info = proxy_info
  33. proxy_info = json.loads(proxy_info)
  34. expire_at = int(proxy_info.get('expired_at'))
  35. # 删除过期的代理
  36. if expire_at <= now:
  37. Proxy.redis.hdel(key, proxy)
  38. proxy_list.remove(proxy)
  39. continue
  40. return proxy
  41. @staticmethod
  42. def del_proxy(proxy):
  43. proxy_info = Proxy.redis.hget('IpProxyHash', proxy)
  44. if proxy_info is None:
  45. return
  46. proxy_info = json.loads(proxy_info)
  47. min_expired_at = proxy_info.get('min_expired_at')
  48. if min_expired_at is None:
  49. min_expired_at = 0
  50. min_expired_at = int(min_expired_at)
  51. now = int(time.time())
  52. if min_expired_at >= now:
  53. return
  54. # 删除失效的代理
  55. print('删除失效代理:' + proxy)