snapshot_jd.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import json
  2. import time
  3. import requests
  4. from commons.Logger import logger
  5. from commons.conn_mysql import MySQLPoolOnline, MySQLPool39
  6. from spiders.jd.jd_auto_crawl_snap2 import JdCrawlerV2
  7. from commons.scheduler import CrawlerScheduler
  8. from commons.feishu_webhook import send_text
  9. import random
  10. from commons.config import JD_DEVICE_ID
  11. platform_name = "京东"
  12. class JdMain:
  13. def __init__(self):
  14. # self.db_online = MySQLPool39()
  15. self.crawl_count = ""
  16. self.task_id = ""
  17. self.task_dict = None
  18. self.driver = None
  19. self.cumulative_pages = 0
  20. self.cumulative_items = 0
  21. self.cumulative_stored = 0
  22. self.cumulative_skipped = 0
  23. def run(self):
  24. spider_schedule = CrawlerScheduler(JD_DEVICE_ID, 2)
  25. spider_schedule.start()
  26. time.sleep(3)
  27. while 1:
  28. if not spider_schedule.end:
  29. self.task_dict = spider_schedule.get_task()
  30. if not self.task_dict:
  31. logger.info(f"{platform_name}暂无任务")
  32. time.sleep(35)
  33. continue
  34. self.task_id = self.task_dict.get("id", "")
  35. self.crawl_count, is_success, self.driver, self.cumulative_pages, self.cumulative_items, self.cumulative_stored, self.cumulative_skipped = JdCrawlerV2(self.task_dict, spider_schedule, self.driver, self.cumulative_pages, self.cumulative_items, self.cumulative_stored, self.cumulative_skipped).run()
  36. spider_schedule.stop()
  37. else:
  38. spider_schedule.start()
  39. print('休息')
  40. time.sleep(35)
  41. time.sleep(10)
  42. if __name__ == '__main__':
  43. while True:
  44. JdMain().run()
  45. interval_time = random.randint(180, 300)
  46. logger.info(f"程序睡眠{interval_time}秒后继续执行")
  47. time.sleep(interval_time)