Text: so-and-so white rice
Source: Python Technology [official ID: PYTHonAll]

Jingdong insurance automation

You must have bought a lot of things on 618, and some stores like to secretly cut prices and make us feel like we lost 100 million in an instant. Today is a JINGdong automatic insurance script.

Get a list of insured prices

First of all, you need to log in jingdong, this in the “618! Jingdong PC edition grab volume” wrote, is the use of two-dimensional code login, you can see.

Open jd insurance page (pcsitepp-fm.jd.com/), you need to get the red box below…

Press F12 to get the next page element

Let’s put the global variables first

user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'

session = requests.session()
Copy the code

Get_pin () gets the PIN value, get_order_list() loops to get the order list, and get_order_list() loops to get the order list. The skuProResultPC() function determines whether the order has exceeded the insured time


def get_pin() :
    """ get the PIN and pull it from the page using a regular expression """ "

    url = "https://pcsitepp-fm.jd.com/"
    r = session.get(url)
    loginPin = re.findall('<input type="hidden" id="loginPin" value="(\w+)" />', r.text)
    pin = loginPin[0] if len(loginPin) > 0 else None
    return pin

def get_order_list(pin, page_num=1) :
    """ Insured list """

    # Store order information
    order_info = []
    # Storage quantity
    count_dir = {}

    url = "https://pcsitepp-fm.jd.com/rest/pricepro/priceskusPull"
    data = {"page": page_num, "pageSize": 10}
    headers = {
        'User-Agent': user_agent,
        'Referer': 'https://pcsitepp-fm.jd.com/',
    }
    r = session.post(url, headers= headers, data=data)

    # The separator between orders
    orders = r.text.split('<tr class="sep-row"><td colspan="6"></td></tr>')
    orders.pop(0)

    for item in orders:
        # order number
        orderid = re.findall("Order no. :(\d+)", item)
        # number
        count = re.findall('<span class="count">\n([\sx\d]+)</span>',item)
        # SkU and serial number of the item
        skuidAndSequences = re.findall("queryOrderSkuPriceParam\.skuidAndSequence\.push\(\"(\d+\,\d+)\"\)\;", item)
        newSkuidAndSequences = []

        # SkU of the item and serial number of the item in the order
        for ss in skuidAndSequences:

            # Determine whether the insurance price of the order has expired
            if skuProResultPC(orderid[0], ss.split(', ') [0], pin):

                newSkuidAndSequences.append(ss)
                if orderid[0] = ='117403228624':
                    print(orderid[0])
                count_ss = count_html[int(ss.split(', ') [1]) - 1]
                count = count_ss.replace('\t'.' ').replace('\n'.' ').replace('x'.' ')
                # use "order number _sku" as key
                count_dir[orderid[0] + '_' + ss.split(', ') [0]] = count

        if newSkuidAndSequences:

            order_info.append({'orderid': orderid[0].'skuidAndSequence': newSkuidAndSequences})

    if orders:
        """ Get all goods recursively """ "
        bill_info_sub, count_dir_sub = get_order_list(pin, page_num + 1)
        order_info.extend(bill_info_sub)
        count_dir.update(count_dir_sub)
    return order_info, count_dir

def skuProResultPC(orderId, skuId, pin) :
    """ """ """ """ """

    url = "https://sitepp-fm.jd.com/rest/webserver/skuProResultPC"
    data = {
        "orderId": orderId,
        "skuId": skuId,
        "pin": pin
    }
    headers = {
        'User-Agent': user_agent,
        'Referer': 'https://pcsitepp-fm.jd.com/',
    }

    r = session.post(url, data=data, headers=headers)
    return 'overTime' not in r.text
Copy the code

The sample results

Order information: [{'orderid': '118598307636'.'skuidAndSequence': ['851267, 1'] {},'orderid': '118597642910'.'skuidAndSequence': ['4389188, 1'] {},'orderid': '123625659522'.'skuidAndSequence': ['69699954984, 1'] {},'orderid': '123539553604'.'skuidAndSequence': ['6839875, 1'] {},'orderid': '123474129967'.'skuidAndSequence': ['50000970588, 1']}] Quantity of goods {'118598307636 _851267': '10'.'118597642910 _4389188': '10'.'123625659522 _69699954984': '1'.'123539553604 _6839875': '4'.'123474129967 _50000970588': '1'.'122842702447 _31295640649': '1'.'117403228624 _31295640649': '1'}
Copy the code

Commodity information

In the list of obtaining the insurance price, the SKU number of the product has been obtained. In the product URL (such as: item.jd.com/6839875.htm… Sku number, the product information can be obtained by skU number.

In the page’s JS, the pageConfig variable has all the data we need, which is extracted using a regular expression

def get_product_info(skuId) :
    """ Obtain commodity information """
    info = {}
    url = "http://item.jd.com/%s.html" % skuId
    headers = {
        'User-Agent': user_agent,
        'Referer': 'https://pcsitepp-fm.jd.com/',
    }
    r = requests.get(url, headers=headers)
    pageConfig = re.findall("var pageConfig = \{([\s\S]+)\} catch\(e\) \{\}", r.text)
    cat = re.findall("cat: \[([\d,]+)\]", pageConfig[0])
    venderId = re.findall("venderId:(\d+)", pageConfig[0])
    shopId = re.findall("shopId:'(\d+)'", pageConfig[0])
    name = re.findall("name: '(.+)'", pageConfig[0])
    info['cat'] = cat[0] if len(cat) else ""
    info['venderId'] = venderId[0] if len(venderId) else ""
    info['shopId'] = shopId[0] if len(shopId) else ""
    info['skuId'] = skuId
    The default delivery area is Beijing
    info['area'] = '1 _72_55653_0' 
    info['name'] = name[0]
    return info
Copy the code

The sample results

{'cat': '1319152, 5705, 7'.'venderId': '1000015389'.'shopId': '1000015389'.'skuId': '6839875'.'area': '1 _72_55653_0'.'name': Chiaus Thin C Gravity Diaper Small S27 (4-8kg) diapers for newborns}
{'cat': '1319631, 3156, 14'.'venderId': '10109166'.'shopId': '965779'.'skuId': '50000970588'.'area': '1 _72_55653_0'.'name': "Doctor colorful baby shaped pillow 0-1 year old summer breathable Tenss newborn latex pillow baby U pillow newborn products green tenss pillow"}
{'cat': '1319152 7155'.'venderId': '107952'.'shopId': '106328'.'skuId': '31295640649'.'area': '1 _72_55653_0'.'name': Babycare babycare 108 cigarettes *6 packs}
Copy the code

Current commodity price

After obtaining the product information, submit the data as parameters to the URL (c0.3.cn/stock) to obtain the product…

def get_product_price(product_info) :

    url = "Https://c0.3.cn/stock?skuId= & area = {} {} & venderId = {} & buyNum = 1 & choseSuitSkuIds = & cat = {} & extraParam = {{% 22 originid % 22:221% % 22 }}&fqsp=0&ch=1&callback=jQuery{}"\.format(product_info['skuId'], 
                product_info['area'], 
                product_info['venderId'], 
                product_info.get('cat'.' '), 
                random.randint(1000000.9999999))
    headers = {
        'User-Agent': user_agent,
        'Host': 'c0.3. Cn'.'Referer':  'https://item.jd.com/{0}.html'.format(product_info['skuId']),
    }
    r = session.get(url, headers=headers)
    data = parse_json(r.text)

    Price of #
    price = data.get("stock", {}).get("jdPrice", {}).get('p'.0)
    return float(price)
Copy the code

Goods can be coupons

When shopping on JD.com, there are often coupons with full xx minus XX and full XX with xx discount. When you do not use coupons when buying goods, you can contact customer service for price protection. Grab the coupons and print them out here

def get_product_coupon(product_info, price) :
    """ Coupon List """
    result = []
    headers = {
        'User-Agent': user_agent,
        'Referer':  'https://item.jd.com/{0}.html'.format(product_info['skuId']),
    }
    url = 'https://cd.jd.com/promotion/v2?callback=jQuery{}&skuId={}&area={}&shopId={}&venderId={}&cat={}&isCanUseDQ=1&isCanUseJQ= 1&platform=0&orgType=2&jdPrice={}&appid=1&_={}'\.format(
                str(random.randint(1000000.9999999)),
                product_info['skuId'],
                product_info['area'],
                product_info['shopId'],
                product_info['venderId'],
                product_info['cat'].replace(', '.'%2C'),
                price,
                str(int(time.time() * 1000)))
    r = session.get(url, headers=headers)
    data = parse_json(r.text)
    pickOneTag = data.get("prom", {}).get("pickOneTag")

    # with reduction
    if pickOneTag:
        for tag in pickOneTag:
            result.append(tag.get('content'))

    # discount
    skuCoupon = data.get('skuCoupon')
    if skuCoupon:
        for coupon in skuCoupon:
            if coupon.get('allDesc'):
                result.append(coupon.get('allDesc'))
            elif coupon.get('quota') and coupon.get('discount'):
                result.append("Full" + str(coupon.get('quota')) + 'cut' + str(coupon.get('discount')))
    return result
Copy the code

The sample results

['Cash reduction of $100 for every $199 up to a maximum of $1000'.'Full 3 pieces, 50% off the total price'.'at 105.0 minus 5.0']
Copy the code

Order price

At this point, the current price, coupon and purchase quantity of the goods have been obtained, and the order price of the remaining order needs to be obtained, which is the amount in the first picture. We use the order number and SKU number as parameters to batch query the order price

def get_price_list(pin) :
    Get order price, item information, current price, quantity

    product_list = []

    Take order number, SKU and item quantity
    queryOrderPriceParam,count_dir = get_order_list(pin)

    # Get the purchase price
    params = {"queryOrderPriceParam": json.dumps(queryOrderPriceParam)}
    r = session.post("https://sitepp-fm.jd.com/rest/webserver/getOrderListSkuPrice", data = params)
    orderList = r.json()

    for item in orderList:

        skuid = item.get("skuid")
        buyingjdprice = item.get("buyingjdprice")
        orderid = item.get("orderid")

        # Product information
        product_info = get_product_info(skuid)
        # Current price
        price = get_product_price(product_info)
        # coupons
        coupon = get_product_coupon(product_info, price)

        name = product_info['name']
        count = count_dir[orderid + '_' + skuid]

        product_list.append({'orderid': orderid, 'name': name, 'price': price, 'coupon': coupon, 'count': count, 'buyingjdprice': buyingjdprice})
    return product_list
Copy the code

The sample results

[{' chiAUS ': '123539553604', 'name': 'ChiAUS thin C Gravity Diaper Small S27 (4-8kg) ', 'price': 27.9,' Coupon ': [' preferential buy 1 piece can buy goods sell like hot cakes', 'full 105.0 5.0', '199 minus 50', '268 minus 60],' count ', '4', 'buyingjdprice: 27.0}, {' orderid' : '123474129967', 'Name ':' Doctor Of Seven Colors Baby Stylize Pillow 0-1 year old Summer Breathe Tenss Baby Latex Pillow Baby U pillow Baby Products Green Tenss Pillow ', 'price': 69.0, 'coupon': [' 1', 'buyingjdprice'] : [' 1', 'buyingjdprice': 66.0}]Copy the code

Apply for price protection

The last step is to compare prices and apply for price protection. There are two situations. One is that you can apply for price protection directly without coupons, and the other is that there are coupons, which can be printed to the console to inform users.

def protect_protect_apply(product_list) :
    """ Apply for price protection """

    if len(product_list) == 0:
        return
    else:
        for item in product_list:
            result = 'Order no. : {}, Name: {}, Quantity: {}, Purchase price: {}, Current price: {}, current offer: {}. '\.format(item['orderid'],
                        item['name'],
                        item['count'],
                        item['buyingjdprice'],
                        item['price'].'|'.join(item['coupon']))

            # No coupons and purchase price higher than current price
            if len(item['coupon'= =])0 and item['buyingjdprice'] > item['price']:

                url = 'https://pcsitepp-fm.jd.com//rest/pricepro/skuProtectApply'
                data = {
                    "orderId": item['orderId']."orderCategory": "Others"."skuId": item['skuId']."refundtype": 1
                }

                headers = {
                    'User-Agent': user_agent,
                    'Referer': 'https://pcsitepp-fm.jd.com/'.'accept': 'application/json, text/javascript, */*; Q = 0.01 '
                }
                session.post(url, data=data, headers=headers)
                print(result + 'Price protection has been applied, please check the price protection page.')

            elif len(item['coupon') >0:
                print(result + 'Do not apply for automatic price protection on coupons, please contact customer service to apply')
    return
Copy the code

The sample results

Order No. : 123539553604, Name: Chiaus Thin C gravity Diaper small S27 (4-8kg) diapers for newborn soft and thin breathable, Quantity: 4, Purchase price: 27.0, current price: 27.9, current discount: Preferential buy 1 piece can buy goods sell like hot cakes | full 105.0 5.0 | 199 | 268 50 and 60 reduction. In the coupon did not apply for automatic price protection, please contact customer service to apply for order no. : 123474129967, Name: Seven color Doctor baby shaped pillow 0-1 year old summer permeable Tencel neonatal latex pillow baby U pillow newborn products green tencel pillow, Quantity: 1, purchase price: 66.0, current price: 69.0, current discount: June 16 00:00 this product participate in the cross-store full discount activity, full 2 pieces, the total price is 20% off; Full 3 pieces, total 7 discount | full 98 minus 5 minus 5.0 | | full 105.0 full 178 minus 10. If automatic price protection is not applied in the coupon, please contact customer service to applyCopy the code

conclusion

Jingdong automatic pricing script core code has been completed, you can change the code according to their needs. Such as adding scheduled tasks, sending emails instead of printing, and so on.

Welcome to the wechat official account: Python Technology, here we have personally written 100 days of practical training, a variety of interesting programming practices, a variety of learning materials, and a large group of lovely friends to discuss with each other.