Open In App
Related Articles

Amazon product availability checker using Python

Improve Article
Save Article
Like Article

As we know Python is a multi-purpose language and widely used for scripting. Its usage is not just limited to solve complex calculations but also to automate daily life task. Let’s say we want to track any Amazon product availability and grab the deal when the product availability changes and inform the user of availability through email. It will be a great fun to write a Python script for this.
Note: Install the required libraries (as per the code) before running the script. Also, note if the product is not available currently then no email will be sent to user. Asin Id should be provided by the user for the product he wants to keep track of.

Working of each module used: 
-> requests: Used to make HTTP get and post requests 
-> time: Used to find current time, wait, sleep 
-> schedule: Used to schedule a function to run again after intervals. It is similar to “setInterval” functionality in JavaScript. 
-> smptlib: Used to send email using Python.

Below is the implementation of above project:


# Python script for Amazon product availability checker
# importing libraries
from lxml import html
import requests
from time import sleep
import time
import schedule
import smtplib
# Email id for who want to check availability
receiver_email_id = "EMAIL_ID_OF_USER"
def check(url):
    headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36'}
    # adding headers to show that you are
    # a browser who is sending GET request
    page = requests.get(url, headers = headers)
    for i in range(20):
        # because continuous checks in
        # milliseconds or few seconds
        # blocks your request
        # parsing the html content
        doc = html.fromstring(page.content)
        # checking availability
        XPATH_AVAILABILITY = '//div[@id ="availability"]//text()'
        AVAILABILITY = ''.join(RAw_AVAILABILITY).strip() if RAw_AVAILABILITY else None
        return AVAILABILITY
def sendemail(ans, product):
    recipient = receiver_email_id
    body_of_email = ans
    email_subject = product + ' product availability'
    # creates SMTP session
    s = smtplib.SMTP('', 587)
    # start TLS for security
    # Authentication
    # message to be sent
    headers = "\r\n".join(["from: " + GMAIL_USERNAME,
                        "subject: " + email_subject,
                        "to: " + recipient,
                        "mime-version: 1.0",
                        "content-type: text/html"])
    content = headers + "\r\n\r\n" + body_of_email
    s.sendmail(GMAIL_USERNAME, recipient, content)
def ReadAsin():
    # Asin Id is the product Id which
    # needs to be provided by the user
    Asin = 'B077PWK5BT'
    url = "" + Asin
    print ("Processing: "+url)
    ans = check(url)
    arr = [
        'Only 1 left in stock.',
        'Only 2 left in stock.',
        'In stock.']
    if ans in arr:
        # sending email to user if
        # in case product available
        sendemail(ans, Asin)
# scheduling same code to run multiple
# times after every 1 minute
def job():
while True:
    # running all pending tasks/jobs


Only 1 left in stock.
Only 1 left in stock.
Only 1 left in stock.

Note that the program might throw an error (Critical security alert/Sign-in attempt was blocked) while sending the mail to the user, which can be handled by modifying the security setting in the mail application you are using. 

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 02 Nov, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials