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 anyAmazon 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 Idshould 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:
Python3# Python script for Amazon product availability checker# importing librariesfromlxmlimporthtmlimportrequestsfromtimeimportsleepimporttimeimportscheduleimportsmtplib# Email id for who want to check availabilityreceiver_email_id="EMAIL_ID_OF_USER"defcheck(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 requestpage=requests.get(url,headers=headers)foriinrange(20):# because continuous checks in# milliseconds or few seconds# blocks your requestsleep(3)# parsing the html contentdoc=html.fromstring(page.content)# checking availabilityXPATH_AVAILABILITY='//div[@id ="availability"]//text()'RAw_AVAILABILITY=doc.xpath(XPATH_AVAILABILITY)AVAILABILITY=''.join(RAw_AVAILABILITY).strip()ifRAw_AVAILABILITYelseNonereturnAVAILABILITYdefsendemail(ans,product):GMAIL_USERNAME="YOUR_GMAIL_ID"GMAIL_PASSWORD="YOUR_GMAIL_PASSWORD"recipient=receiver_email_idbody_of_email=ansemail_subject=product+' product availability'# creates SMTP sessions=smtplib.SMTP('smtp.gmail.com',587)# start TLS for securitys.starttls()# Authentications.login(GMAIL_USERNAME,GMAIL_PASSWORD)# message to be sentheaders="\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_emails.sendmail(GMAIL_USERNAME,recipient,content)s.quit()defReadAsin():# Asin Id is the product Id which# needs to be provided by the userAsin='B077PWK5BT'url="https://www.amazon.in/dp/"+Asinprint("Processing: "+url)ans=check(url)arr=['Only 1 left in stock.','Only 2 left in stock.','In stock.']print(ans)ifansinarr:# sending email to user if# in case product availablesendemail(ans,Asin)# scheduling same code to run multiple# times after every 1 minutedefjob():print("Tracking....")ReadAsin()schedule.every(1).minutes.do(job)whileTrue:# running all pending tasks/jobsschedule.run_pending()time.sleep(1)
Output:
Tracking....Processing: https://www.amazon.in/dp/B077PWK5BTOnly 1 left in stock.Tracking....Processing: https://www.amazon.in/dp/B077PWK5BTOnly 1 left in stock.Tracking....Processing: https://www.amazon.in/dp/B077PWK5BTOnly 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.
Amazon product availability checker by using Python