Skip to content

Instantly share code, notes, and snippets.

@preetesh33
Created October 15, 2023 11:03
Show Gist options
  • Save preetesh33/1310a0b1cb37ae2992aca955f58f63dd to your computer and use it in GitHub Desktop.
Save preetesh33/1310a0b1cb37ae2992aca955f58f63dd to your computer and use it in GitHub Desktop.
Amazon price drop notification code
import requests
from bs4 import BeautifulSoup
import smtplib
import time
import sys
# enable secure login in gmail using url https://www.google.com/settings/security/lesssecureapps
#### add expected price
expected_price = 5000
#### add product URL from amazon
URL="https://www.amazon.in/gp/product/B01MV5C18G/ref=ox_sc_act_image_1?smid=A2XCP9SIL6EKIY&psc=1"
def get_price():
headers = {'User-Agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"}
page = requests.get(URL,headers=headers)
soup = BeautifulSoup(page.content,'html.parser')
title = soup.find(id="productTitle").get_text().strip()
#print(title)
price_find = soup.find(id="twister-plus-price-data-price-unit").get_text().encode('utf8')
price = str(price_find).split(":")[2].split(",")[0].split(".")[0]
print(price)
amazon_price= int(price)
#print(soup.prettify().encode('utf8'))
if amazon_price <= expected_price:
send_email()
sys.exit(0)
else:
print("price is still at - {}".format(amazon_price))
################## aad your email id password
def send_email():
FROM = 'xxxxx@gmail.com'
TO = "xxxxx@hotmail.com"
SUBJECT = 'Amazon Price Went Down'
TEXT = URL
# Prepare actual message
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login("username", "password")
server.sendmail(FROM, TO, message)
server.close()
print('successfully sent the mail')
except Exception as e:
print(e)
# code runs every hr
while(True):
get_price()
time.sleep(3600) # every hr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment