Tracking Amazon Prices for Free
How to use Python, GroupMe, and Google Cloud to track pricing changes on Amazon.
The Problem:
I want to be notified when a product on Amazon goes down in price. But the websites that do offer this service require $$ or have lots of restrictions.
The Plan:
- Program a Python script that continuously tracks a webpage for any price changes
- Add a feature to be notified through a GroupMe chat bot.
- Have the program run on a Google Cloud Computing server for free
- Save $$
Python Programming:
We’re going to need a few things first.
- A good text editor. I’d recommend Visual Studio Code (VSC)
- If you are not on a MacOS computer, download Python3
- Download the Python extension for VSC
- In the terminal window in VSC, type
pip install selenium
- Download the chrome webdriver: here
- Unzip the file, and you should be good to go. If there are any problems on windows follow this guide
In order to track the elements on a website, we will be using Selenium & the ChromeDriver, but there are other options like bs4.
Create a new python file. It can be named anything, but just make sure you have the .py extension at the end
ex. amazon_price.py
Importing Libraries:
import requests
from selenium import webdriver
import selenium as se
from selenium.common.exceptions import NoSuchElementException
import time
from time import sleep
from random import randint
Setting Up Chrome Driver:
options = se.webdriver.ChromeOptions()
chrome is set to headless
options.add_argument('headless')
driver = se.webdriver.Chrome(options=options)
Getting the Web Page & Price:
Place Amazon product you want to track
driver.get("https://www.amazon.com/Sony-Noise-Cancelling-Headphones-WH1000XM3/dp/B07G4MNFS1/ref=sr_1_2?crid=N5OCS4NJDH4M&keywords=sony+wh-1000xm3&qid=1551040801&s=gateway&sprefix=sony+%2Caps%2C120&sr=8-2")
sets base price once
global_base_price = driver.find_element_by_xpath('//*[@id="priceblock_ourprice"]').text
Creating Check Changes Function:
def check_change_by_xpath(xpath, base_price):
try:
# refreshes the page, finds the price
# if the price changed, the current price is returned
driver.refresh
current_price = driver.find_element_by_xpath('//*[@id="priceblock_ourprice"]').text
if current_price != base_price:
return current_price
except requests.exceptions.RequestException as e:
sleep(60)
return False
The Loop:
while True:
current_state = check_change_by_xpath('//*[@id="priceblock_ourprice"]', global_base_price)
print("curr state ", current_state)
if current_state != False:
global_base_price = current_state
sleep(randint(3,5))
Now we’re done writting the Python script, let’s try running it!
In terminal, python3 name_of_file.py
If everything worked out with no errors, lets move on to getting GroupMe setup.
GroupMe:
Go to the GroupMe dev page here, and have a Groupme account and make a group with only you in it.\
- Click on the Bots tab -> login -> Create Bot
- Select a group, name the bot something, and you can leave the other fields blank
-
copy the bot id and you can copy and paste it into the code below:
post_params = { 'bot_id' : 'insert_botID_here', 'text': "your string response" } requests.post('https://api.groupme.com/v3/bots/post', params = post_params)
- Place the cope snippet anywhere you want to send a message to your phone
Now Your Done!!!
For my version of the finished product go to my github repository