Skip to content

Commit

Permalink
抢购流程调整为多进程
Browse files Browse the repository at this point in the history
  • Loading branch information
huanghyw committed Dec 11, 2020
1 parent 658f45f commit 7aeea21
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
57 changes: 54 additions & 3 deletions jd_mask_spider_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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等"""
Expand Down
16 changes: 9 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
@@ -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 = """
Expand All @@ -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)

0 comments on commit 7aeea21

Please sign in to comment.