DEV Community

Cover image for Building an Amazon Price Tracker with Python and WayScript
Sunil Aleti
Sunil Aleti

Posted on • Updated on

Building an Amazon Price Tracker with Python and WayScript

Hi
Well, Today we will discuss How to built Amazon Price Tracker by scraping the price details and scheduling with WayScript

Lemme tell you the real reason to build this ;)
I'm a foodie I love Nutella 🤤 but due to this pandemic, I couldn't find Nutella near me in any local stores. So I thought to order it in Amazon but the prices are damn high and prices are fluctuating too... then I started to build a tracker which notifies me whenever the price goes down the tracking price.

Let's get started..

We gonna create a python script which uses request and beautifulSoup module to scrape the data and scheduling the script to run every hour either with Serverless AWS Lambda or WayScript but AWS Lambda is not my cup of tea. So I built using WayScript.

We will discuss this process in 2 phases.
Phase I : Scraping the product details
Phase II : Scheduling the script to run every hour

Phase I:

Step 1: Create an excel sheet with urls and Tracking Price

Alt Text

Step 2: import necessary packages/module

import requests
import bs4
import pandas as pd
Enter fullscreen mode Exit fullscreen mode

Request - The requests module allows you to send HTTP requests using Python
bs4 - Beautiful Soup is a library that makes it easy to scrape information from web
pandas - pandas is a fast and powerful used for data analysis.
we are faking ourselves as a Firefox user to avoid restrictions.

HEADERS = ({'User-Agent':
            'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
            'Accept-Language': 'en-US, en;q=0.5'})
Enter fullscreen mode Exit fullscreen mode

Step 3: Building Python Script
Here is the tracker function which takes parameters URL and TrackingPrice and adds the details of the product when it is less or than equal to TrackingPrice


def tracker(url,TrackingPrice):
    res = requests.get(url,headers=HEADERS)
    soup = bs4.BeautifulSoup(res.content, features='lxml')

    # to prevent script from crashing when there isn't a price for the product
    try:
        title = soup.find(id="productTitle").get_text().strip()
        amount = float(soup.find(id='priceblock_ourprice').get_text().replace("₹","").replace("$","").strip())
        if amount<=TrackingPrice:
            offer.append("You got a offer on the {0} for {1}. Check out the product {2}".format(title,amount,url))


    except:
        offer.append("Couldn't get details about product")
Enter fullscreen mode Exit fullscreen mode



Source Code:

import requests
import bs4
import pandas as pd


HEADERS = ({'User-Agent':
            'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
            'Accept-Language': 'en-US, en;q=0.5'})
offer=[]
def tracker(url,TrackingPrice):
    res = requests.get(url,headers=HEADERS)
    soup = bs4.BeautifulSoup(res.content, features='lxml')

    # to prevent script from crashing when there isn't a price for the product
    try:
        title = soup.find(id="productTitle").get_text().strip()
        amount = float(soup.find(id='priceblock_ourprice').get_text().replace("₹","").replace("$","").strip())
        if amount<=TrackingPrice:
            offer.append("You got a offer on the {0} for {1}. Check out the product {2}".format(title,amount,url))


    except:
        offer.append("Couldn't get details about product")



df=pd.read_csv("https://docs.google.com/spreadsheets/d/1AzJ93zR6--4vwl81W3v0FHyZ_bFMkFYRxOSjodJu_Qw/export?format=csv")
for i in range(0,len(df["URL"])):
    tracker(df["URL"][i],df["TrackingPrice"][i])
outputs["message"]=offer

Enter fullscreen mode Exit fullscreen mode

Output:

Alt Text

we're done with our python script

Now let's move to Phase II

Phase II:

I already told you can schedule this script either by using AWS Lambda or any other cloud computing services as I'm not familar with AWS. I'm going with WayScript

And this WayScript provide wide range of packages to interact with services
Ex:

  • Gmail
  • Text Message
  • Charts
  • Figma
  • Slack
  • Trello
  • Twitter

Step 1: Open WayScript

Packages used for this scheduling:

  • Time Trigger
  • Python
  • Loop
  • Text Message

Step 2: Explaining everything in blog is not possible and people get cumbersome. So I made video of "How to configure the Python Script in WayScript.
Kindly Follow the video

Click here to check my configuration in WayScript

On one lucky day..

The price got reduced and this script notified me by sending a text message and I ordered it 😁

Alt Text


If you like my content, please consider supporting me

Buy Me A Coffee

Hope it's useful
A ❤️ would be Awesome 😊

Discussion (12)

Collapse
illgamer profile image
Carlos Solorzano

I tried to build the price checker for some GPUs but after a few request while testing one link mind you manually I started getting Amazon error page telling me to use their API. How did you make it so that you were not blocked?

Collapse
sunilaleti profile image
Sunil Aleti Author

I never got an error page like that..

Collapse
illgamer profile image
Carlos Solorzano

Oh, thank you anyway, I learned a lot about building this type of apps.

Thread Thread
sunilaleti profile image
Sunil Aleti Author

Glad you liked it!!

Collapse
sandordargo profile image
Sandor Dargo

Lol, that's the best reason I've ever read to build a tool! Great job!

Collapse
sunilaleti profile image
Sunil Aleti Author

Thanks 😂

Collapse
ruyaa473 profile image
ruyaa473

Hello, I wonder if you can make a program that adds more than one product, tracks it and notifies us by e-mail? we would be grateful

Collapse
battuubelin profile image
BattuUBelin

Hi, sorry, it gives me this error:
line 30, in
outputs["message"]=offer
NameError: name 'outputs' is not defined
Why?
THX a lot!

Collapse
sunilaleti profile image
Sunil Aleti Author

This code works when you run in wayscript not in terminal
As wayscript expects some list variable for output, so code is written wrt wayscript

Collapse
battuubelin profile image
BattuUBelin

Thanks, sorry I'm a noob! Now on Wayscript I have only this output: ["Couldn't get details about product"]
But there is a price...