Amazon product availability checker using Python

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 similiar to “setInterval” functionality in JavaScript.
-> smptlib: Used to send email using Python.

Below is the implementation of above project:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
        sleep(3
          
        # parsing the html content
        doc = html.fromstring(page.content)
          
        # checking availaility
        XPATH_AVAILABILITY = '//div[@id ="availability"]//text()'
        RAw_AVAILABILITY = doc.xpath(XPATH_AVAILABILITY)
        AVAILABILITY = ''.join(RAw_AVAILABILITY).strip() if RAw_AVAILABILITY else None
        return AVAILABILITY
  
      
def sendemail(ans, product):
    GMAIL_USERNAME = "YOUR_GMAIL_ID"
    GMAIL_PASSWORD = "YOUR_GMAIL_PASSWORD"
      
    recipient = receiver_email_id
    body_of_email = ans
    email_subject = product + ' product availability'
      
    # creates SMTP session 
    s = smtplib.SMTP('smtp.gmail.com', 587
      
    # start TLS for security 
    s.starttls() 
      
    # Authentication 
    s.login(GMAIL_USERNAME, GMAIL_PASSWORD) 
      
    # 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)
    s.quit() 
  
  
def ReadAsin():
    # Asin Id is the product Id which 
    # needs to be provided by the user
    Asin = 'B077PWK5BT' 
    url = "http://www.amazon.in/dp/" + Asin
    print ("Processing: "+url)
    ans = check(url)
    arr = [
        'Only 1 left in stock.',
        'Only 2 left in stock.',
        'In stock.']
    print(ans)
    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():
    print("Tracking...."
    ReadAsin()
  
schedule.every(1).minutes.do(job)
  
while True:
      
    # running all pending tasks/jobs
    schedule.run_pending() 
    time.sleep(1)

chevron_right



Output:

Tracking....
Processing: http://www.amazon.in/dp/B077PWK5BT
Only 1 left in stock.
Tracking....
Processing: http://www.amazon.in/dp/B077PWK5BT
Only 1 left in stock.
Tracking....
Processing: http://www.amazon.in/dp/B077PWK5BT
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.



My Personal Notes arrow_drop_up

Competitive Programmer, Full Stack Developer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.