Home > Mobile >  How to requests all sizes in stock - Python
How to requests all sizes in stock - Python

Time:05-22

I'm trying to request all the sizes in stock from Zalando. I can not quite figure out how to do it since the video I'm watching showing how to request sizes look different than min. The video that I watch was this. Video - 5.30

Does anyone know how to request the sizes in stock and print the sizes that in stock?

The site in trying to request sizes of: here

My code looks like this:

import requests
from bs4 import BeautifulSoup as bs

session = requests.session()

def get_sizes_in_stock():
    global session
    endpoint = "https://www.zalando.dk/nike-sportswear-air-max-90-sneakers-ni112o0bt-a11.html"
    response = session.get(endpoint)

    soup = bs(response.text, "html.parser")

I have tried to go to the View page source and look for the sizes, but I could not see the sizes in the page source.

I hope someone out there can help me what to do.

CodePudding user response:

The sizes are in the page

I found them in the html, in a javascript tag, in the format

{
    "sku": "NI112O0BT-A110090000",
    "size": "42.5",
    "deliveryOptions": [
        {
            "deliveryTenderType": "FASTER"
        }
    ],
    "offer": {
        "price": {
            "promotional": null,
            "original": {
                "amount": 114500
            },
            "previous": null,
            "displayMode": null
        },
        "merchant": {
            "id": "810d1d00-4312-43e5-bd31-d8373fdd24c7"
        },
        "selectionContext": null,
        "isMeaningfulOffer": true,
        "displayFlags": [],
        "stock": {
            "quantity": "MANY"
        },
        "sku": "NI112O0BT-A110090000",
        "size": "42.5",
        "deliveryOptions": [
            {
                "deliveryTenderType": "FASTER"
            }
        ],
        "offer": {
            "price": {
                "promotional": null,
                "original": {
                    "amount": 114500
                },
                "previous": null,
                "displayMode": null
            },
            "merchant": {
                "id": "810d1d00-4312-43e5-bd31-d8373fdd24c7"
            },
            "selectionContext": null,
            "isMeaningfulOffer": true,
            "displayFlags": [],
            "stock": {
                "quantity": "MANY"
            }
        },
        "allOffers": [
            {
                "price": {
                    "promotional": null,
                    "original": {
                        "amount": 114500
                    },
                    "previous": null,
                    "displayMode": null
                },
                "merchant": {
                    "id": "810d1d00-4312-43e5-bd31-d8373fdd24c7"
                },
                "selectionContext": null,
                "isMeaningfulOffer": true,
                "displayFlags": [],
                "stock": {
                    "quantity": "MANY"
                },
                "deliveryOptions": [
                    {
                        "deliveryWindow": "2022-05-23 - 2022-05-25"
                    }
                ],
                "fulfillment": {
                    "kind": "ZALANDO"
                }
            }
        ]
    }
}

If you parse the html with bs4 you should be able to find the script tag and extract the JSON.

CodePudding user response:

The sizes for the default color of shoe are shown in html. Alongside this are the urls for the other colors. You can extract these into a dictionary and loop, making requests and pulling the different colors and their availability, which I think is what you are actually requesting, as follows (note: I have kept quite generic to avoid hardcoding keys which change across requests):

import requests, re, json

def get_color_results(link):
    headers = {"User-Agent": "Mozilla/5.0"}
    r = requests.get(link, headers=headers).text
    data = json.loads(re.search(r'(\{"enrichedEntity".*size.*)<\/script', r).group(1))
    results = []
    color = ""
    for i in data["graphqlCache"]:
        if "ern:product" in i:
            if "product" in data["graphqlCache"][i]["data"]:
                if "name" in data["graphqlCache"][i]["data"]["product"]:
                    results.append(data["graphqlCache"][i]["data"]["product"])
                if (
                    color == ""
                    and "color" in data["graphqlCache"][i]["data"]["product"]
                ):
                    color = data["graphqlCache"][i]["data"]["product"]["color"]["name"]
    return (color, results)


link = "https://www.zalando.dk/nike-sportswear-air-max-90-sneakers-ni112o0bt-a11.html"
final = {}
color, results = get_color_results(link)
colors = {
    j["node"]["color"]["name"]: j["node"]["uri"]
    for j in [
        a
        for b in [
            i["family"]["products"]["edges"]
            for i in results
            if "family" in i
            if "products" in i["family"]
        ]
        for a in b
    ]
}
final[color] = {
    j["size"]: j["offer"]["stock"]["quantity"]
    for j in [i for i in results if "simples" in i][0]["simples"]
}

for k, v in colors.items():
    if k not in final:
        color, results = get_color_results(v)
        final[color] = {
            j["size"]: j["offer"]["stock"]["quantity"]
            for j in [i for i in results if "simples" in i][0]["simples"]
        }

print(final)
  • Related