Посмотрев на вкладку сети в вашем браузере, вы найдете JSON
, который содержит все продукты - вы можете загрузить эти данные, просто добавив .json
к URL-адресу. Тем не менее, он не содержит информации о запасах, поскольку эти данные загружаются из магазина, который, по-видимому, является канадской международной компанией электронной коммерции со штаб-квартирой в Оттаве (согласно Википедии). Эти данные загружаются в тег скрипта в DOM, который вы можете легко извлечь с помощью BeautifulSoup (хотя я на самом деле не использую его - мы могли бы просто использовать регулярные выражения).
from bs4 import BeautifulSoup
import requests
import re
# define both URLs: for the JSON and the actual website for the shopify stock
url = 'https://limitededt.com/collections/footwear/products/adidas-originals-jonah-hill-superstar-fw7577'
product_info = url + '.json'
# fetch both data sources
with requests.Session() as session:
soup = BeautifulSoup(session.get(url).text, 'html.parser')
swym = soup.find('script', {'id': 'swym-snippet'})
info = session.get(product_info).json()
# this will contain all the products and their stock
products = dict()
# get the data for each product from the json
for variant in info['product']['variants']:
products[variant['id']] = variant
# find the shopify data using a regular expression
regex = re.compile(r'SwymProductVariants\[[0-9]+\] = ({[^(;)]+)')
inventory = re.findall(regex, swym.text)
# add the inventory information to the previously constructed product dictionary
for inv in inventory:
indices = [key.strip().split(':')[1] for index, key in enumerate(inv.split(',')) if index in [1, 5]]
products[int(indices[0])]['stock'] = int(indices[1])