Automating Discount Notifications with Python and Beautiful Soup
Written on
Chapter 1: Introduction to Automated Price Notifications
For some time now, I've been searching for a laptop, keeping a specific price point in mind. However, the process of shopping online can often feel like a lengthy endeavor, especially when aiming to snag a bargain.
Rather than manually checking websites every day, I decided to create a Python script that would automatically alert me via email when the laptop's price dipped below my desired threshold.
Article Objective
In this piece, I'll illustrate how we can streamline our online shopping experience by automating the process of tracking discounted prices for our desired products using Python.
Web scraping is a valuable skill. By utilizing web scraping techniques, we can extract data from websites and minimize our manual efforts. Below, I've included a Python script that can be tailored to scrape various websites and products as needed.
Understanding the principles of web scraping can empower us to develop robust data-driven applications, which is why I encourage everyone to learn how it functions.
Chapter 2: The Power of Python and Beautiful Soup
To facilitate this task, I employed a Python library called Beautiful Soup. This library excels at extracting data from HTML and XML documents and can be paired with a parser for effective navigation, searching, and modifications. Essentially, Beautiful Soup simplifies the web scraping process.
Python stands out as a powerful programming language. If you're interested in learning Python thoroughly, I highly recommend exploring resources that present the language in an accessible manner.
1. Installing Required Libraries
Begin by installing the following dependency:
bs4==0.0.12
2. Crafting the Main Script: main.py
The script consists of two classes: PriceNotifier, which is initialized with an instance of EmailSender. The PriceNotifier includes a method called notify_if_cheap, which takes in the URL of the product page to monitor and a target price. It will send me an email notification if the product's price falls below the specified target.
import requests
from bs4 import BeautifulSoup
from email.message import EmailMessage
import smtplib
class PriceNotifier:
def __init__(self, email_sender):
self._email_sender = email_sender
def notify_if_cheap(self, url, target_price):
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
current_price = float(soup.find_all('div', {'class': 'cf-price'})[2].getText().strip().replace(',', '')[1:])
if current_price < target_price:
self._email_sender.send(url, current_price)
class EmailSender:
def __init__(self, email_id, email_password, to):
self._email_pass = email_password
self._email_id = email_id
self._to = to
def send(self, url, price):
message = EmailMessage()
message['Subject'] = "Price Alert: Time To Purchase"
message['From'] = self._email_id
message['To'] = self._to
message.set_content(f"The product {url} is now available at your target price of {price}.")
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(self._email_id, self._email_pass)
smtp.send_message(message)
3. Executing the Script (in main.py)
if __name__ == "__main__":
target_price = 8000
email_id = "[email protected]"
email_password = "your_password"
to = "[email protected]"
price_notifier = PriceNotifier(EmailSender(email_id, email_password, to))
price_notifier.notify_if_cheap(target_url, target_price)
By running this script, I can track the price of my desired product and receive notifications when it falls below my set threshold. Additionally, this script can be scheduled to run automatically at designated intervals.
Summary
Web scraping is a valuable skill that allows us to extract data from various online sources and automate manual tasks. I hope this script proves useful for anyone seeking to gather information from web pages.
Chapter 3: Practical Applications and Resources
In this video titled "Build an E-Commerce Discount Notification App [Python, Bright Data, WhatsApp]," learn how to create an app that alerts users about discounts on e-commerce platforms.
The second video, "Automatic Email Alerts | Python For Finance Episode 5," demonstrates how to set up automatic email notifications for financial updates using Python.