如何用 Python 在京东上抢口罩

def submit_order(session, risk_control, sku_id, skuids, submit_Time, encryptClientInfo, is_Submit_captcha, payment_pwd,

                 submit_captcha_text, submit_captcha_rid):

    # 提交端口的url

    url = 'https://trade.jd.com/shopping/order/submitOrder.action'


# 提交参数 data = { 'overseaPurchaseCookies': '', 'vendorRemarks': '[]', 'submitOrderParam.sopNotPutInvoice': 'false', 'submitOrderParam.trackID': 'TestTrackId', 'submitOrderParam.ignorePriceChange': '0', 'submitOrderParam.btSupport': '0', 'riskControl': risk_control, 'submitOrderParam.isBestCoupon': 1, 'submitOrderParam.jxj': 1, 'submitOrderParam.trackId': '9643cbd55bbbe103eef18a213e069eb0', # Todo: need to get trackId 'submitOrderParam.needCheck': 1, }
# 如果用到京豆会需要输入支付密码 def encrypt_payment_pwd(payment_pwd): return ''.join(['u3' + x for x in payment_pwd])
# 校验支付密码 if len(payment_pwd) > 0: data['submitOrderParam.payPassword'] = encrypt_payment_pwd(payment_pwd)
# 请求报文头 headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/531.36", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3", "Referer": "http://trade.jd.com/shopping/order/getOrderInfo.action", "Connection": "keep-alive", 'Host': 'trade.jd.com', }
# 订单提交会尝试两次 for count in range(1, 3): logger.info('第[%s/%s]次尝试提交订单', count, 3) try: # 可能会存在的校验码 if is_Submit_captcha: captcha_result = page_detail_captcha(session, encryptClientInfo) # 验证码服务错误 if not captcha_result: logger.error('验证码服务异常') continue data['submitOrderParam.checkcodeTxt'] = submit_captcha_text data['submitOrderParam.checkCodeRid'] = submit_captcha_rid # 提交订单 resp = session.post(url=url, data=data, headers=headers) resp_json = json.loads(resp.text) logger.info('本次提交订单耗时[%s]毫秒', str(int(time.time() * 1000) - submit_Time)) # 判断是否提交成功 if resp_json.get('success'): logger.info('订单提交成功! 订单号:%s', resp_json.get('orderId')) return True else: # 提交失败返回的多种原因 resultMessage, result_code = resp_json.get('message'), resp_json.get('resultCode') if result_code == 0: # self._save_invoice() if '验证码不正确' in resultMessage: resultMessage = resultMessage + '(验证码错误)' logger.info('提交订单验证码[错误]') continue else: resultMessage = resultMessage + '(下单商品可能为第三方商品,将切换为普通发票进行尝试)' elif result_code == 60077: resultMessage = resultMessage + '(可能是购物车为空 或 未勾选购物车中商品)' elif result_code == 60123: resultMessage = resultMessage + '(需要在payment_pwd参数配置支付密码)' elif result_code == 60070: resultMessage = resultMessage + '(省份不支持销售)' skuids.remove(sku_id) logger.info('[%s]类型口罩不支持销售', sku_id) logger.info('订单提交失败, 错误码:%s, 返回信息:%s', result_code, resultMessage) logger.info(resp_json) return False except Exception as e: print(traceback.format_exc())

continue