Photo by Hitesh Choudhary on Unsplash
Building an Automated Skincare Price Tracker with Python and Selenium: A Step-by-Step Guide
Hello and welcome to my blog. I have procrastinated doing this for a little while.
I am a skincare junkie. To say the least
So every month, i have to search and buy skincare products and i decided to make my life easier. The goal was to build a script that could scrape product prices, calculate the total cost, and send a summary email to me. This article walks through the process of building such an automation tool using Python, Selenium, and email integration. The website used is Teeka4, it is an African online skincare store where i usually get my products.
Key Technologies Used:
Python: The programming language used for this project.
Selenium: For web scraping and interacting with the website.
Pandas: For data manipulation and calculations.
SMTP (smtplib): For sending the email summary.
Jupyter notebook: For writing the codes
Step 1: Installing Anaconda Navigator
Anaconda is a popular distribution of Python that comes with various scientific libraries and a user-friendly environment for managing packages and environments.
Download Anaconda:
Go to the Anaconda Downloads page and select the version suitable for your operating system (Windows, macOS, or Linux).Install Anaconda:
Follow the installation instructions for your OS. Once installed, you can launch it which provides a graphical interface for managing environments and launching applications like Jupyter Notebook which we will be using here.
Step 2: Creating a Virtual Environment
Using a virtual environment helps you keep dependencies organized and ensures that your project has all the necessary packages without interfering with other Python projects.
Open Anaconda Navigator.
In the Environments tab, click on Create.
Name your environment (e.g.,
skincare-tracker
) and select the version of Python you’d like to use (Python 3.8 or later is recommended, it will usually automatically select the latest).Click Create, and it will set up a fresh environment for you.
Step 3: Installing Required Libraries
After creating the environment, we need to install the libraries we’ll use in this project:
Select your newly created environment from the list in Anaconda Navigator.
Click on the Channels tab, then use the Search Packages bar to install the following libraries:
pandas
beautifulsoup4
selenium
smptlib
time
datetime
Alternatively, you can install these dependencies directly from the command line by activating your environment and running:
conda activate skincare-tracker
pip install pandas beautifulsoup4 selenium smtplib
Step 4: Launching Jupyter Notebook
In Anaconda Navigator, go to the Home tab.
Under your environment (
skincare-tracker
), find Jupyter Notebook and click Launch.Jupyter Notebook will open in your browser, where you can create a new notebook or open an existing one.
Step 5: The Project
1. Importing Necessary Libraries:
from bs4 import BeautifulSoup
import requests
import selenium
import smtplib
import time
import datetime
import csv
import pandas as pd
import ssl
from email.message import EmailMessage
from selenium import webdriver
In this first step, we import several libraries to help us with web scraping (selenium
), data manipulation (pandas
), sending emails (smtplib
), and working with dates (datetime
).
2. Defining Skincare Products List:
my_skincare = ['Anua Niacinamide 10% + TXA 4% Serum 30ml', 'Anua Heartleaf Pore Control Cleansing Oil 200', ...]
for skin_product in my_skincare:
print(skin_product)
Here, we define a list of skincare products. These are the products we’ll search for on the e-commerce website. The for
loop prints each product to confirm the list.
3. Setting Up the Web Driver (Selenium):
driver = webdriver.Chrome()
url = "https://teeka4.com/"
driver.get(url)
We initialize the Selenium Chrome WebDriver and navigate to the website. This allows us to interact with the page, just like a human would.
4. Searching for Products and Scraping Prices:
for each_skincare in my_skincare:
search_bar = driver.find_element(By.NAME, "s")
search_bar.send_keys(each_skincare)
search_bar.send_keys(Keys.RETURN)
results = driver.find_elements(By.CLASS_NAME, "price")
for result in results:
price = result.text
data = [each_skincare, price, today]
with open(f"Skincare_list_on_{today}.csv", "a+", newline='', encoding='UTF8') as f:
writer = csv.writer(f)
writer.writerow(data)
This block loops through each product in our list and performs the following actions:
Locates the search bar and enters the product name.
Presses the
RETURN
key to search.Finds all elements with the class name "price" and extracts the price.
Saves the product name, price, and current date into a CSV file.
5. Handling "Add to Cart" and Exceptions:
try:
add_to_cart = driver.find_element(By.NAME, "add-to-cart")
add_to_cart.send_keys(Keys.RETURN)
print("added to cart")
except NoSuchElementException as e:
print(f"{each_skincare} is not available")
Here, we check if the "Add to Cart" button is available. If it is, we simulate clicking it. If not, we catch the exception and print a message saying the product is unavailable.
6. Sending the Email:
email_sender = # your email
recipient_email = # the email you want to sent it to
password = "your password"
body = f"""
Hiya Olivia,
Here is your total price for your skincare this month: {formatted_total}.
This is the breakdown:
{formatted_text}
View your cart: {cart_link}
"""
new_message = EmailMessage()
new_message['From'] = email_sender
new_message['To'] = recipient_email
new_message['Subject'] = f"Skincare for {formatted_date}"
new_message.set_content(body)
context = ssl.create_default_context()
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as smtp:
try:
smtp.login(email_sender, password)
smtp.send_message(new_message)
print(f"Email sent successfully.")
except smtplib.SMTPException as e:
print(f"Error sending email: {e}")
After gathering the data, I send an email to myself with the total price and a breakdown of the products. This section:
Creates the email body with the total price and product list.
Uses
smtplib
to send the email through Gmail’s SMTP server.
8. Data Cleanup and Final Price Calculation:
df = pd.read_csv(f"Skincare_list_on_{today}.csv")
df['Price'] = df['Price'].str.replace('₦', '', regex=False)
df['Price'] = df['Price'].str.replace(',', '')
df['Price'] = pd.to_numeric(df['Price'])
total_price = df['Price'].sum()
formatted_total = f"₦{total_price:,}"
Before calculating the total, we clean the price data by removing unwanted symbols and commas. Then, we calculate the total price using pandas
.
Challenges & Lessons Learned
Attention to Detail is Critical
Web scraping requires precision because even minor changes in a website’s structure (like a new class name or element position) can break the script.
Debugging takes time, and I had to frequently inspect elements and adjust the script.
This Might Not Be the Most Efficient Approach
While using Selenium works for interacting with the website, it can be slow and resource-intensive.
Alternatives like using an API (if available) or parsing static HTML with
requests
andBeautifulSoup
could be more efficient.
It's a Learning Process
- Despite the challenges, this project helped me understand how to automate web-based tasks, handle structured data, and integrate email notifications.
Conclusion:
This project was a great opportunity for me to practice Python, web scraping, and automation. By automating mundane tasks, we can save time and improve productivity. I hope this article helps you looking to automate your tasks or build similar projects!.