ProxyRefreshSchedule.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. import sys
  20. sys.path.append('../')
  21. from Manager.ProxyManager import ProxyManager
  22. class ProxyRefreshSchedule(ProxyManager):
  23. """
  24. 代理定时刷新
  25. """
  26. def __init__(self):
  27. ProxyManager.__init__(self)
  28. def validProxy(self):
  29. self.db.changeTable(self.raw_proxy_queue)
  30. raw_proxy = self.db.pop()
  31. while raw_proxy:
  32. proxies = {"http": "http://{proxy}".format(proxy=raw_proxy),
  33. "https": "https://{proxy}".format(proxy=raw_proxy)}
  34. try:
  35. r = requests.get('https://www.baidu.com/', proxies=proxies, timeout=50, verify=False)
  36. if r.status_code == 200:
  37. self.db.changeTable(self.useful_proxy_queue)
  38. self.db.put(raw_proxy)
  39. except Exception as e:
  40. # print e
  41. pass
  42. self.db.changeTable(self.raw_proxy_queue)
  43. raw_proxy = self.db.pop()
  44. def refreshPool():
  45. pp = ProxyRefreshSchedule()
  46. pp.validProxy()
  47. def main(process_num=100):
  48. p = ProxyRefreshSchedule()
  49. p.refresh()
  50. for num in range(process_num):
  51. P = Process(target=refreshPool, args=())
  52. P.start()
  53. print '{time}: refresh complete!'.format(time=time.ctime())
  54. if __name__ == '__main__':
  55. # pp = ProxyRefreshSchedule()
  56. # pp.main()
  57. main()
  58. sched = BlockingScheduler()
  59. sched.add_job(main, 'interval', minute=20)
  60. sched.start()