snapshot_jd.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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.db_online = MySQLPoolOnline()
  16. self.crawl_count = ""
  17. self.task_id = ""
  18. self.task_dict = None
  19. self.driver = None
  20. self.cumulative_pages = 0
  21. self.cumulative_items = 0
  22. self.cumulative_stored = 0
  23. self.cumulative_skipped = 0
  24. def run(self):
  25. spider_schedule = CrawlerScheduler(JD_DEVICE_ID, 2)
  26. spider_schedule.start()
  27. time.sleep(3)
  28. while 1:
  29. if not spider_schedule.end:
  30. self.task_dict = spider_schedule.get_task()
  31. if not self.task_dict:
  32. logger.info(f"{platform_name}暂无任务")
  33. time.sleep(35)
  34. continue
  35. self.task_id = self.task_dict.get("id", "")
  36. 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()
  37. spider_schedule.stop()
  38. else:
  39. spider_schedule.start()
  40. print('休息')
  41. time.sleep(35)
  42. time.sleep(10)
  43. if __name__ == '__main__':
  44. while True:
  45. JdMain().run()
  46. interval_time = random.randint(180, 300)
  47. logger.info(f"程序睡眠{interval_time}秒后继续执行")
  48. time.sleep(interval_time)