From 7aeea215130c2df07e2ecd3dbf6f649ea5e9d847 Mon Sep 17 00:00:00 2001 From: HuangYiwei Date: Fri, 11 Dec 2020 12:03:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=A2=E8=B4=AD=E6=B5=81=E7=A8=8B=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E4=B8=BA=E5=A4=9A=E8=BF=9B=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jd_mask_spider_requests.py | 57 ++++++++++++++++++++++++++++++++++++-- main.py | 16 ++++++----- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/jd_mask_spider_requests.py b/jd_mask_spider_requests.py index f7e761ef..b6b6c9c8 100644 --- a/jd_mask_spider_requests.py +++ b/jd_mask_spider_requests.py @@ -6,9 +6,10 @@ import requests from util import parse_json, get_session, get_sku_title,send_wechat from config import global_config +from concurrent.futures import ProcessPoolExecutor -class Jd_Mask_Spider(object): +class JdSecKill(object): def __init__(self): # 初始化信息 self.session = get_session() @@ -19,6 +20,56 @@ def __init__(self): self.timers = Timer() self.default_user_agent = global_config.getRaw('config', 'DEFAULT_USER_AGENT') + def reserve(self): + """ + 预约 + """ + self.__reserve() + + def seckill(self): + """ + 抢购 + """ + self.__seckill() + + def wati_some_time(self): + time.sleep(random.randint(100, 300) / 1000) + + def seckill_by_proc_pool(self, work_count=3): + """ + 多进程进行抢购 + work_count:进程数量 + """ + with ProcessPoolExecutor(work_count) as pool: + for i in range(work_count): + pool.submit(self.seckill) + self.wati_some_time() + + def __reserve(self): + """ + 预约 + """ + self.login() + while True: + try: + self.make_reserve() + except Exception as e: + logger.info('预约发生异常!', e) + self.wati_some_time() + + def __seckill(self): + """ + 抢购 + """ + while True: + try: + self.request_seckill_url() + self.request_seckill_checkout_page() + self.submit_seckill_order() + except Exception as e: + logger.info('抢购发生异常!', e) + self.wati_some_time() + def login(self): for flag in range(1, 3): try: @@ -122,8 +173,8 @@ def get_seckill_url(self): logger.info("抢购链接获取成功: %s", seckill_url) return seckill_url else: - logger.info("抢购链接获取失败,%s不是抢购商品或抢购页面暂未刷新,0.5秒后重试") - time.sleep(0.1) + logger.info("抢购链接获取失败,稍后自动重试") + self.wati_some_time() def request_seckill_url(self): """访问商品的抢购链接(用于设置cookie等""" diff --git a/main.py b/main.py index 946d8f87..a8bfe1aa 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,8 @@ import sys -from jd_mask_spider_requests import Jd_Mask_Spider +from jd_mask_spider_requests import JdSecKill + + + if __name__ == '__main__': a = """ @@ -10,16 +13,15 @@ / /__ / . \ |__| |1.预约商品 /_____|/_/ \_\____/ 2.秒杀抢购商品 """ - start_tool = Jd_Mask_Spider() print(a) + + jd_seckill = JdSecKill() choice_function = input('选择功能:') if choice_function == '1': - start_tool.login() - start_tool.make_reserve() + jd_seckill.reserve() elif choice_function == '2': - start_tool.request_seckill_url() - start_tool.request_seckill_checkout_page() - start_tool.submit_seckill_order() + jd_seckill.seckill_by_proc_pool() else: print('没有此功能') sys.exit(1) +