From 52597178a86fe050d0f9b4e93e6dde0b963458c8 Mon Sep 17 00:00:00 2001 From: bvwl <2201101122@qq.com> Date: Sun, 30 Nov 2025 09:15:08 +0800 Subject: [PATCH] 0.2.2 --- spider/main.py | 19 ++++++++++++++++--- spider/proxys.py | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/spider/main.py b/spider/main.py index 8405fb9..705f3b3 100644 --- a/spider/main.py +++ b/spider/main.py @@ -675,6 +675,19 @@ def is_forbidden_time() -> bool: end = now.replace(hour=20, minute=0, second=0, microsecond=0) return start <= now < end +def wait_until_out_of_forbidden(interval_sec: float = 5.0, stop_event: threading.Event | None = None) -> None: + """ + 在禁跑时段内循环等待,直到禁跑时段结束 + + 参数: + interval_sec: 轮询间隔秒数 + stop_event: 可选停止事件,若设置则在等待期间可提前结束 + """ + while is_forbidden_time(): + if stop_event is not None and stop_event.wait(timeout=interval_sec): + break + time.sleep(interval_sec) + def seconds_until(hour: int, minute: int) -> float: """ @@ -782,9 +795,8 @@ def main(): if stop_event.wait(timeout=300): continue cleanup_all_browsers() - secs = seconds_until(20, 0) - logger.info(f"处于禁跑时段,休眠至20:00,剩余 {int(secs)} 秒") - time.sleep(secs) + logger.info("处于禁跑时段,等待至禁跑结束") + wait_until_out_of_forbidden() continue executor = ThreadPoolExecutor(max_workers=len(proxies)) @@ -815,6 +827,7 @@ def main(): pass time.sleep(300) cleanup_all_browsers() + wait_until_out_of_forbidden() break for f, proxy in list(futures_map.items()): if f.done() and not stop_event.is_set() and not restart_event.is_set(): diff --git a/spider/proxys.py b/spider/proxys.py index 1cf2d21..414ad40 100644 --- a/spider/proxys.py +++ b/spider/proxys.py @@ -93,4 +93,4 @@ hz = [ ] -proxy_list = xy \ No newline at end of file +proxy_list = work \ No newline at end of file