getter.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from proxypool.tester import Tester
  2. from proxypool.db import RedisClient
  3. from proxypool.crawler import Crawler
  4. from proxypool.setting import *
  5. class Getter():
  6. def __init__(self):
  7. self.redis = RedisClient()
  8. self.tester = Tester()
  9. self.crawler = Crawler()
  10. def is_over_threshold(self):
  11. """
  12. 判断是否达到了代理池限制
  13. """
  14. if self.redis.count() >= POOL_UPPER_THRESHOLD:
  15. return True
  16. else:
  17. return False
  18. def run(self):
  19. print('获取器开始执行')
  20. proxy_count = 0
  21. while not self.is_over_threshold():
  22. for callback_label in range(self.crawler.__CrawlFuncCount__):
  23. callback = self.crawler.__CrawlFunc__[callback_label]
  24. # 获取代理
  25. proxies = self.crawler.get_proxies(callback)
  26. # 设置代理并测试
  27. self.tester.set_proxies(proxies)
  28. self.tester.run()
  29. proxy_count += len(proxies)
  30. if self.is_over_threshold():
  31. print('代理池已满,暂停抓取')
  32. break
  33. if proxy_count == 0:
  34. # 代理池枯竭
  35. print('代理池已枯竭')