Python Amazon price tracker














































Python Amazon price tracker



                                  Amazon Price tracker

In this article, we are going to build out a project using python. If you are a python beginner or learning web scraping this simple application will give you some good practice.
we are going to create a script that will track the price of an item and whenever the price of that item falls below the level you want, the script will send you an email. you can choose any product from any website to track using this script.

Requirements:
Python
Beautiful Soup

pip install bs4
Requests

pip install requests
firstly we are going to import the required libraries, and we will provide the link to the product you want to track.


import requests
from bs4 import BeautifulSoup
import smtplib
import time
URL = 'url of the product'

Note: Not every website allows web scraping

Now, we will define headers which will be a dictionary"User-Agent" and provide your browser's user agent details, you can search your user-agent by typing my user agent on google, and load the page using "requests.get()" method.


headers = {"User-Agent": 'your-browser-user-agent'}

page = requests.get(URL, headers=headers)


We will extract now useful information using Beautiful Soup and then extract the name and the price of our item using the id.


soup = BeautifulSoup(page.content, 'html-parser')

title = soup.find(id="productTitle").get_text()
price = soup.find(id="pricebloc_ourprice").get_text()

converted_price = float(price[0:5])


Now we will wrap all this inside a function and make a condition statement so that whenever the price falls in that range the script sends the email.


def check_price():

    page = requests.get(URL, headers=headers)

    soup = BeautifulSoup(page.content, 'html-parser')

    title = soup.find(id="productTitle").get_text()
    price = soup.find(id="pricebloc_ourprice").get_text()

    converted_price = float(price[0:5])

    if(converted_price < "your_price"):
       sendmail()


Now, we will define "sendmail()" function which will sen us an email whenever the price drops and reaches below our defined limit. to perform this function enable the two-step verification of your email


def sendmail():
    server = smtlib.SMTP('smtp.gmail.com', 587)
    servor.ehlo()
    server.starttls()
    server.ehlo()
   
    server.login('username@gmail.com', 'password')
    
    subject = 'Price fell down'
    body = 'check the link (link)'

    msg = f"Subject: {subject}\n\n{body}"

    server.sendmail('username@gmail.com', msg)
    
    server.quit()


Next, we create a while loop that runs indefinitely and calls check_price function after every hour


while(True):
     check_price()
     time.sleep(60 * 60)

FULL CODE:


import requests
from bs4 import BeautifulSoup
import smtplib

URL = 'url of the product'

headers = {"User-Agent": 'your-browser-user-agent'}

def check_price():

    page = requests.get(URL, headers=headers)

    soup = BeautifulSoup(page.content, 'html-parser')

    title = soup.find(id="productTitle").get_text()
    price = soup.find(id="pricebloc_ourprice").get_text()

    converted_price = float(price[0:5])

    if(converted_price < "your_price"):
       sendmail()

def sendmail():
    server = smtlib.SMTP('smtp.gmail.com', 587)
    servor.ehlo()
    server.starttls()
    server.ehlo()
   
    server.login('username@gmail.com', 'password')
    
    subject = 'Price fell down'
    body = 'check the link (link)'

    msg = f"Subject: {subject}\n\n{body}"

    server.sendmail('username@gmail.com', msg)
    
    server.quit()

while(True):
     check_price()
     time.sleep(60 * 60)



Comments