-
Scrape product availability from Otto Germany using Python
Otto is one of the most popular e-commerce platforms in Germany, known for its wide variety of products ranging from fashion to electronics. Scraping product availability from Otto involves using Python with the requests and BeautifulSoup libraries to fetch and parse the HTML content of a product page. Product availability is typically displayed as a label near the “Add to Cart” button, indicating whether the product is “In Stock,” “Out of Stock,” or available for delivery within a specific timeframe.
The first step is to inspect the structure of the webpage using browser developer tools to identify the exact HTML tags and classes containing the availability information. The script sends an HTTP request to the product page, parses the HTML response, and locates the availability text. Error handling is included to address cases where availability information may not be present or is dynamically loaded. Below is a complete implementation for scraping product availability from Otto Germany:import requests from bs4 import BeautifulSoup # URL of the Otto product page url = "https://www.otto.de/product-page" # Headers to mimic a browser request headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" } # Fetch the page content response = requests.get(url, headers=headers) if response.status_code == 200: soup = BeautifulSoup(response.content, "html.parser") # Extract product availability availability = soup.find("div", class_="availability-status") if availability: print("Product Availability:", availability.text.strip()) else: print("Availability information not found.") else: print(f"Failed to fetch the page. Status code: {response.status_code}")
Log in to reply.