Issue
I'm trying to make a function that retrieves the productStockLevel: "inStock"
that is in the data variable on the auchan site on the page of an item.
I'm stuck because I can't access the data variable and I searched on the internet but I couldn't find anything or maybe I used it wrong.
For the moment I can only get the script[4] where my data variable is.
If someone can help me I would be very grateful I've been working on it for 1h30.
code:
import requests
from bs4 import BeautifulSoup
session = requests.session()
def get_stock():
global session
productPage = 'https://www.auchan.fr/power-a-etui-de-protection-silhouette-pikachu-nintendo-switch/p-c1334312'
response = session.get(productPage)
soup = BeautifulSoup(response.content, "html.parser")
scripts = soup.find_all('script')[4]
print(scripts)
get_stock()
Solution
Try:
import re
import json
import requests
url = "https://www.auchan.fr/power-a-etui-de-protection-silhouette-pikachu-nintendo-switch/p-c1334312"
html_doc = requests.get(url).text
data = re.search(r"var product = (.*);", html_doc).group(1)
data = json.loads(data)
# uncomment this to print all data:
# print(json.dumps(data, indent=4))
print("Stock level status:", data["stock"]["stockLevelStatus"]["code"])
Prints:
Stock level status: inStock
The data
variable contains other information as well:
{
"code": "C1334312",
"name": "\u00c9tui de Protection Silhouette Pikachu Nintendo Switch",
"url": "/power-a-etui-de-protection-silhouette-pikachu-nintendo-switch/p-c1334312",
"description": "C\u00e9l\u00e9brez votre amour de Pok\u00e9mon avec cet \u00e9tui de protection sous licence officielle qui stocke la console Nintendo Switch en mode portable. L'\u00e9tui pr\u00e9sente la silhouette de Pikachu, une doublure en feutre et un rabat de protection d'\u00e9cran rembourr\u00e9 avec rangement pour le jeu.",
"purchasable": true,
"stock": {
"stockLevelStatus": {
"code": "inStock",
"type": "StockLevelStatus"
},
"stockLevel": 86,
"stockThreshold": null,
"supplyDelay": 3
},
"futureStocks": null,
"availableForPickup": null,
"averageRating": null,
"numberOfReviews": null,
"summary": "",
"manufacturer": null,
"variantType": null,
"price": {
"currencyIso": "EUR",
"value": 10.0,
...and so on.
Answered By - Andrej Kesely
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.