MCPcopy
hub / github.com/tychxn/jd-assistant / get_single_item_stock

Method get_single_item_stock

jd_assistant.py:421–475  ·  view source on GitHub ↗

获取单个商品库存状态 :param sku_id: 商品id :param num: 商品数量 :param area: 地区id :return: 商品是否有货 True/False

(self, sku_id, num, area)

Source from the content-addressed store, hash-verified

419 return page
420
421 def get_single_item_stock(self, sku_id, num, area):
422 """获取单个商品库存状态
423 :param sku_id: 商品id
424 :param num: 商品数量
425 :param area: 地区id
426 :return: 商品是否有货 True/False
427 """
428 area_id = parse_area_id(area)
429
430 cat = self.item_cat.get(sku_id)
431 vender_id = self.item_vender_ids.get(sku_id)
432 if not cat:
433 page = self._get_item_detail_page(sku_id)
434 match = re.search(r'cat: \[(.*?)\]', page.text)
435 cat = match.group(1)
436 self.item_cat[sku_id] = cat
437
438 match = re.search(r'venderId:(\d*?),', page.text)
439 vender_id = match.group(1)
440 self.item_vender_ids[sku_id] = vender_id
441
442 url = 'https://c0.3.cn/stock'
443 payload = {
444 'skuId': sku_id,
445 'buyNum': num,
446 'area': area_id,
447 'ch': 1,
448 '_': str(int(time.time() * 1000)),
449 'callback': 'jQuery{}'.format(random.randint(1000000, 9999999)),
450 'extraParam': '{"originid":"1"}', # get error stock state without this param
451 'cat': cat, # get 403 Forbidden without this param (obtained from the detail page)
452 'venderId': vender_id # return seller information with this param (can't be ignored)
453 }
454 headers = {
455 'User-Agent': self.user_agent,
456 'Referer': 'https://item.jd.com/{}.html'.format(sku_id),
457 }
458
459 resp_text = ''
460 try:
461 resp_text = requests.get(url=url, params=payload, headers=headers, timeout=self.timeout).text
462 resp_json = parse_json(resp_text)
463 stock_info = resp_json.get('stock')
464 sku_state = stock_info.get('skuState') # 商品是否上架
465 stock_state = stock_info.get('StockState') # 商品库存状态:33 -- 现货 0,34 -- 无货 36 -- 采购中 40 -- 可配货
466 return sku_state == 1 and stock_state in (33, 40)
467 except requests.exceptions.Timeout:
468 logger.error('查询 %s 库存信息超时(%ss)', sku_id, self.timeout)
469 return False
470 except requests.exceptions.RequestException as request_exception:
471 logger.error('查询 %s 库存信息发生网络请求异常:%s', sku_id, request_exception)
472 return False
473 except Exception as e:
474 logger.error('查询 %s 库存信息发生异常, resp: %s, exception: %s', sku_id, resp_text, e)
475 return False
476
477 @check_login
478 def get_multi_item_stock(self, sku_ids, area):

Callers 1

Calls 4

_get_item_detail_pageMethod · 0.95
parse_area_idFunction · 0.90
parse_jsonFunction · 0.90
getMethod · 0.80

Tested by

no test coverage detected