Price-drop alert with Python
Prices go up and down every few days, especially around sales. Instead of checking a product page every morning, you can let a program watch it.
This program takes 30 days of a phone's price, draws a small chart, finds the lowest price and tells you whether today's price has reached your target. Set your target below and press Run.
Track a phone's price for 30 days and get told when to buy.
The Python behind it · 22 lines
1target = 21500 # alert me when the phone drops to this price (₹)23# 30 days of a phone's price (sample; a scraper would fetch this daily)4prices = [24999, 24999, 24499, 24999, 23999, 23999, 24499, 22999, 22999, 23499,5 23999, 23999, 22499, 22999, 23499, 21999, 21999, 22999, 23499, 23999,6 22999, 21499, 21999, 22499, 22999, 20999, 21499, 21999, 22499, 21999]78low, high = min(prices), max(prices)9spark = "▁▂▃▄▅▆▇█"10line = "".join(spark[round((p - low) / (high - low) * 7)] for p in prices)11print(f"30 days {line}")12print(f"High ₹{high:,} · Low ₹{low:,} on day {prices.index(low) + 1} · Today ₹{prices[-1]:,}")1314drops = [d + 1 for d in range(1, 30) if prices[d] < prices[d - 1]]15print(f"Price dropped on {len(drops)} days, usually right after a rise\n")1617if prices[-1] <= target:18 print(f"🔔 ALERT: ₹{prices[-1]:,} is at or below your ₹{target:,}. Buy now!")19else:20 hits = [d + 1 for d, p in enumerate(prices) if p <= target]21 print(f"Waiting: today is ₹{prices[-1] - target:,} above your target.")22 print(f"It hit your price on {len(hits)} of the last 30 days: {hits or 'never'}")
How the code works, step by step
1.The price history
A list holds one price per day. This is sample data; a real tracker would add one price each day by fetching the product page.
3# 30 days of a phone's price (sample; a scraper would fetch this daily)4prices = [24999, 24999, 24499, 24999, 23999, 23999, 24499, 22999, 22999, 23499,5 23999, 23999, 22499, 22999, 23499, 21999, 21999, 22999, 23499, 23999,6 22999, 21499, 21999, 22499, 22999, 20999, 21499, 21999, 22499, 21999]2.Draw a sparkline
Each price is scaled between the lowest and highest to a number from 0 to 7, which picks one of eight block characters. The result is a tiny chart in a single line of text.
8low, high = min(prices), max(prices)9spark = "▁▂▃▄▅▆▇█"10line = "".join(spark[round((p - low) / (high - low) * 7)] for p in prices)11print(f"30 days {line}")12print(f"High ₹{high:,} · Low ₹{low:,} on day {prices.index(low) + 1} · Today ₹{prices[-1]:,}")3.Count the drops
A list comprehension compares each day with the day before and keeps the days the price fell.
14drops = [d + 1 for d in range(1, 30) if prices[d] < prices[d - 1]]15print(f"Price dropped on {len(drops)} days, usually right after a rise\n")4.Decide whether to alert
If today's price is at or below your target, print the alert. Otherwise say how far off it is and on which days in the last month it did reach your price.
17if prices[-1] <= target:18 print(f"🔔 ALERT: ₹{prices[-1]:,} is at or below your ₹{target:,}. Buy now!")19else:20 hits = [d + 1 for d, p in enumerate(prices) if p <= target]21 print(f"Waiting: today is ₹{prices[-1] - target:,} above your target.")22 print(f"It hit your price on {len(hits)} of the last 30 days: {hits or 'never'}")
Make it yours
- Fetch today's price from a product page with requests and BeautifulSoup (on your own computer).
- Save each day's price to a CSV so the history builds itself.
- Send yourself the alert by email or Telegram instead of printing it.
Questions
- Can this check Amazon or Flipkart prices directly?
- Not from inside the browser, because shopping sites block requests from other websites. Run the scraping part with Python on your computer and check the site's terms first; many shops prefer you to use their official alerts or API.
- Is the price data real?
- No, it is a sample 30-day history typed into the program, so the result is the same for everyone.