def _get_QRcode_ticket(self): url = 'https://qr.m.jd.com/check' payload = { 'appid': '133', 'callback': 'jQuery{}'.format(random.randint(1000000, 9999999)), 'token': self.sess.cookies.get('wlfstk_smdl'), '_': str(int(time.time() * 1000)), } headers = { 'User-Agent': self.user_agent, 'Referer': 'https://passport.jd.com/new/login.aspx', } resp = self.sess.get(url=url, headers=headers, params=payload) if not response_status(resp): logger.error('获取二维码扫描结果异常') return False resp_json = parse_json(resp.text) if resp_json['code'] != 200: logger.info('Code: %s, Message: %s', resp_json['code'], resp_json['msg']) return None else: logger.info('已完成手机客户端确认') return resp_json['ticket']
def login_by_username(self): if self.is_login: logger.info('登录成功') return True username = input('账号:') password = input('密码:') if (not username) or (not password): logger.error('用户名或密码不能为空') return False self.username = username data = self._get_login_data() uuid = data['uuid'] auth_code = '' if self._need_auth_code(username): logger.info('本次登录需要验证码') auth_code = self._get_auth_code(uuid) else: logger.info('本次登录不需要验证码') login_url = "https://passport.jd.com/uc/loginService" payload = { 'uuid': uuid, 'version': 2015, 'r': random.random(), } data['authcode'] = auth_code data['loginname'] = username data['nloginpwd'] = encrypt_pwd(password) headers = { 'User-Agent': self.user_agent, 'Origin': 'https://passport.jd.com', } resp = self.sess.post(url=login_url, data=data, headers=headers, params=payload) if not response_status(resp): logger.error('登录失败') return False if not self._get_login_result(resp): return False # login success logger.info('登录成功') self.nick_name = self.get_user_info() self._save_cookies() self.is_login = True return True
def _cancel_select_all_cart_item(self): """取消勾选购物车中的所有商品 :return: 取消勾选结果 True/False """ url = "https://cart.jd.com/cancelAllItem.action" data = { 't': 0, 'outSkus': '', 'random': random.random() # 'locationId' can be ignored } resp = self.sess.post(url, data=data) return response_status(resp)
def get_checkout_page_detail(self): """获取订单结算页面信息 该方法会返回订单结算页面的详细信息:商品名称、价格、数量、库存状态等。 :return: 结算信息 dict """ url = 'http://trade.jd.com/shopping/order/getOrderInfo.action' # url = 'https://cart.jd.com/gotoOrder.action' payload = { 'rid': str(int(time.time() * 1000)), } try: resp = self.sess.get(url=url, params=payload) if not response_status(resp): logger.error('获取订单结算页信息失败') return soup = BeautifulSoup(resp.text, "html.parser") self.risk_control = get_tag_value(soup.select('input#riskControl'), 'value') order_detail = { 'address': soup.find( 'span', id='sendAddr').text[5:], # remove '寄送至: ' from the begin 'receiver': soup.find( 'span', id='sendMobile').text[4:], # remove '收件人:' from the begin 'total_price': soup.find( 'span', id='sumPayPriceId').text[1:], # remove '¥' from the begin 'items': [] } # TODO: 这里可能会产生解析问题,待修复 # for item in soup.select('div.goods-list div.goods-items'): # div_tag = item.select('div.p-price')[0] # order_detail.get('items').append({ # 'name': get_tag_value(item.select('div.p-name a')), # 'price': get_tag_value(div_tag.select('strong.jd-price'))[2:], # remove '¥ ' from the begin # 'num': get_tag_value(div_tag.select('span.p-num'))[1:], # remove 'x' from the begin # 'state': get_tag_value(div_tag.select('span.p-state')) # in stock or out of stock # }) logger.info("下单信息:%s", order_detail) return order_detail except Exception as e: logger.error('订单结算页面数据解析异常(可以忽略),报错信息:%s', e)
def _validate_QRcode_ticket(self, ticket): url = 'https://passport.jd.com/uc/qrCodeTicketValidation' print(url) headers = { 'User-Agent': self.user_agent, 'Referer': 'https://passport.jd.com/uc/login?ltype=logout', } resp = self.sess.get(url=url, headers=headers, params={'t': ticket}) if not response_status(resp): return False resp_json = json.loads(resp.text) if resp_json['returnCode'] == 0: return True else: logger.info(resp_json) return False
def _need_auth_code(self, username): url = 'https://passport.jd.com/uc/showAuthCode' data = { 'loginName': username, } payload = { 'version': 2015, 'r': random.random(), } resp = self.sess.post(url, params=payload, data=data, headers=self.headers) if not response_status(resp): logger.error('获取是否需要验证码失败') return False resp_json = json.loads(resp.text[1:-1]) # ({"verifycode":true}) return resp_json['verifycode']
def _get_QRcode(self): url = 'https://qr.m.jd.com/show' payload = { 'appid': 133, 'size': 147, 't': str(int(time.time() * 1000)), } headers = { 'User-Agent': self.user_agent, 'Referer': 'https://passport.jd.com/new/login.aspx', } resp = self.sess.get(url=url, headers=headers, params=payload) if not response_status(resp): logger.info('获取二维码失败') return False QRCode_file = 'QRcode.png' save_image(resp, QRCode_file) logger.info('二维码获取成功,请打开京东APP扫描') open_image(QRCode_file) return True
def _get_auth_code(self, uuid): image_file = os.path.join(os.getcwd(), 'jd_authcode.jpg') url = 'https://authcode.jd.com/verify/image' payload = { 'a': 1, 'acid': uuid, 'uid': uuid, 'yys': str(int(time.time() * 1000)), } headers = { 'User-Agent': self.user_agent, 'Referer': 'https://passport.jd.com/uc/login', } resp = self.sess.get(url, params=payload, headers=headers) if not response_status(resp): logger.error('获取验证码失败') return '' save_image(resp, image_file) open_image(image_file) return input('验证码:')