ProxyRefreshSchedule.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. """
  4. -------------------------------------------------
  5. File Name: ProxyRefreshSchedule.py
  6. Description : 代理定时刷新
  7. Author : JHao
  8. date: 2016/12/4
  9. -------------------------------------------------
  10. Change Activity:
  11. 2016/12/4: 代理定时刷新
  12. -------------------------------------------------
  13. """
  14. __author__ = 'JHao'
  15. from apscheduler.schedulers.blocking import BlockingScheduler
  16. from multiprocessing import Process
  17. import requests
  18. import time
  19. from Manager.ProxyManager import ProxyManager
  20. class ProxyRefreshSchedule(ProxyManager):
  21. """
  22. 代理定时刷新
  23. """
  24. def __init__(self):
  25. ProxyManager.__init__(self)
  26. def validProxy(self):
  27. self.db.changeTable(self.raw_proxy_queue)
  28. raw_proxy = self.db.pop()
  29. while raw_proxy:
  30. proxies = {"http": "http://{proxy}".format(proxy=raw_proxy),
  31. "https": "https://{proxy}".format(proxy=raw_proxy)}
  32. try:
  33. r = requests.get('https://www.baidu.com/', proxies=proxies, timeout=50, verify=False)
  34. if r.status_code == 200:
  35. self.db.changeTable(self.useful_proxy_queue)
  36. self.db.put(raw_proxy)
  37. except Exception as e:
  38. # print e
  39. pass
  40. self.db.changeTable(self.raw_proxy_queue)
  41. raw_proxy = self.db.pop()
  42. def refreshPool():
  43. pp = ProxyRefreshSchedule()
  44. pp.validProxy()
  45. def main(process_num=100):
  46. p = ProxyRefreshSchedule()
  47. p.refresh()
  48. for num in range(process_num):
  49. P = Process(target=refreshPool, args=())
  50. P.start()
  51. print '{time}: refresh complete!'.format(time=time.ctime())
  52. if __name__ == '__main__':
  53. # pp = ProxyRefreshSchedule()
  54. # pp.main()
  55. main()
  56. sched = BlockingScheduler()
  57. sched.add_job(main, 'interval', minute=20)
  58. sched.start()