DeveloperBreeze

Tutorials Programming Tutorials, Guides & Best Practices

Explore 149+ expertly crafted tutorials tutorials, components, and code examples. Stay productive and build faster with proven implementation strategies and design patterns from DeveloperBreeze.

Building a Web Scraper to Track Product Prices and Send Alerts

Tutorial December 10, 2024
python

import requests
from bs4 import BeautifulSoup

# Target URL of the product
URL = "https://www.example.com/product-page"

# Headers to mimic a real browser
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"
}

def get_product_price():
    response = requests.get(URL, headers=HEADERS)
    soup = BeautifulSoup(response.content, "html.parser")

    # Extract product name and price using CSS selectors
    product_name = soup.find("h1", {"class": "product-title"}).text.strip()
    price = soup.find("span", {"class": "price"}).text.strip()

    return product_name, float(price.replace("$", ""))

product, price = get_product_price()
print(f"{product}: ${price}")

Allow users to specify a desired price threshold and compare it with the current price.