News Feed Forums General Web Scraping How to scrape product availability from an e-commerce website?

  • How to scrape product availability from an e-commerce website?

    Posted by Laura Warda on 12/18/2024 at 9:45 am

    Scraping product availability from e-commerce websites can help track stock levels or analyze product trends. Most sites indicate availability using clear tags or messages like “In Stock” or “Out of Stock.” Using BeautifulSoup, you can extract these details for static pages. For dynamic sites, Puppeteer or Selenium is better suited as they can render JavaScript content. Monitoring network requests can also reveal endpoints that provide stock data directly.
    Here’s an example using BeautifulSoup to scrape product availability:

    import requests
    from bs4 import BeautifulSoup
    url = "https://example.com/products"
    headers = {"User-Agent": "Mozilla/5.0"}
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        soup = BeautifulSoup(response.content, "html.parser")
        products = soup.find_all("div", class_="product-item")
        for product in products:
            name = product.find("h2", class_="product-title").text.strip()
            availability = product.find("span", class_="availability-status").text.strip()
            print(f"Product: {name}, Availability: {availability}")
    else:
        print("Failed to fetch product availability.")
    

    Dynamic content often requires interaction with filters or scrolling to reveal more products. Using proxies and rate-limiting helps avoid being blocked during large-scale scraping. How do you handle inconsistent availability labels across different sites?

    Riaz Lea replied 5 days, 11 hours ago 4 Members · 3 Replies
  • 3 Replies
  • David Maja

    Member
    12/20/2024 at 9:54 am

    I write conditional logic to handle multiple possible labels for availability. For example, checking for “In Stock,” “Available,” or “Add to Cart” ensures nothing is missed.

  • Gualtiero Wahyudi

    Member
    12/25/2024 at 7:56 am

    For sites with dynamic content, I use Puppeteer to load all elements fully before scraping. This ensures I don’t miss products that are loaded asynchronously.

  • Riaz Lea

    Member
    01/17/2025 at 6:24 am

    To avoid being flagged, I randomize request intervals and rotate proxies. These small adjustments make my scraper less detectable.

Log in to reply.