Skip to content

Commit

Permalink
wait_some_time移动到util.py
Browse files Browse the repository at this point in the history
  • Loading branch information
huanghyw committed Dec 22, 2020
1 parent fc6009b commit 02b804e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
13 changes: 5 additions & 8 deletions jd_spider_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from lxml import etree
from jd_logger import logger
from timer import Timer
from util import parse_json, send_wechat, get_session, response_status, save_image, open_image
from util import parse_json, send_wechat, wait_some_time, response_status, save_image, open_image
from config import global_config
from concurrent.futures import ProcessPoolExecutor
from exception import SKException
Expand Down Expand Up @@ -307,9 +307,6 @@ def seckill_by_proc_pool(self, work_count=5):
for i in range(work_count):
pool.submit(self.seckill)

def wait_some_time(self):
time.sleep(random.randint(100, 300) / 1000)

def _reserve(self):
"""
预约
Expand All @@ -319,7 +316,7 @@ def _reserve(self):
self.make_reserve()
except Exception as e:
logger.info('预约发生异常!', e)
self.wait_some_time()
wait_some_time()

def _seckill(self):
"""
Expand All @@ -333,7 +330,7 @@ def _seckill(self):
self.submit_seckill_order()
except Exception as e:
logger.info('抢购发生异常,稍后继续执行!', e)
self.wait_some_time()
wait_some_time()

def make_reserve(self):
"""商品预约"""
Expand Down Expand Up @@ -384,7 +381,7 @@ def get_username(self):
resp = self.session.get(url=url, params=payload, headers=headers)
else:
break
self.wait_some_time()
wait_some_time()
# 响应中包含了许多用户信息,现在在其中返回昵称
# jQuery2381773({"imgUrl":"//storage.360buyimg.com/i.imageUpload/xxx.jpg","lastLoginTime":"","nickName":"xxx","plusStatus":"0","realName":"xxx","userLevel":x,"userScoreVO":{"accountScore":xx,"activityScore":xx,"consumptionScore":xxxxx,"default":false,"financeScore":xxx,"pin":"xxx","riskScore":x,"totalScore":xxxxx}})
return parse_json(resp.text).get('nickName')
Expand Down Expand Up @@ -429,7 +426,7 @@ def get_seckill_url(self):
return seckill_url
else:
logger.info("抢购链接获取失败,稍后自动重试")
self.wait_some_time()
wait_some_time()

def request_seckill_url(self):
"""访问商品的抢购链接(用于设置cookie等"""
Expand Down
5 changes: 5 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import random
import requests
import os
import time

from config import global_config

Expand Down Expand Up @@ -87,6 +88,10 @@ def get_session():
return session


def wait_some_time():
time.sleep(random.randint(100, 300) / 1000)


def send_wechat(message):
"""推送信息到微信"""
url = 'http://sc.ftqq.com/{}.send'.format(global_config.getRaw('messenger', 'sckey'))
Expand Down

0 comments on commit 02b804e

Please sign in to comment.