Building an Amazon Price Tracker with Python and WayScript
Tags: python, webdev, tutorial, productivity
Published: 2020-09-17 09:51:11

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

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.

<h2><b>Let's get started..</b></h2>
<p>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 <b>Serverless AWS Lambda</b> or <b>WayScript</b> but AWS Lambda is not my cup of tea. So I built using WayScript.</p> 

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

<h3><b>Phase I:</b></h3>

<b>Step 1:</b> Create an excel sheet with urls and Tracking Price<br>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5nyu2o51b4r81qpp1r3i.png)

<b>Step 2:</b> import necessary packages/module 
```python
import requests
import bs4
import pandas as pd
```

<b>Request</b> - The requests module allows you to send HTTP requests using Python
<b>bs4</b> - Beautiful Soup is a library that makes it easy to scrape information from web
<b>pandas</b> - pandas is a fast and powerful used for data analysis.
we are faking ourselves as a Firefox user to avoid restrictions.
```python
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'})
```

<b>Step 3:</b> 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
``` python

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")
```
<br>
<b>Source Code:</b>
```python
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

```
<b>Output:</b>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cgp892ev5rtoflitf2vm.PNG)

<br>

we're done with our python script <br>
Now let's move to Phase II

<b>Phase II:</b><br>
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
<b>Ex:</b>
<ul>
<li>Gmail</li>
<li>Text Message</li>
<li>Charts</li>
<li>Figma</li>
<li>Slack</li>
<li>Trello</li>
<li>Twitter</li>
</ul>
<br>

<b>Step 1: </b>Open <a href="https://wayscript.com/">WayScript</a><br>
<b>Packages used for this scheduling:</b>
<ul>
<li>Time Trigger</li>
<li>Python</li>
<li>Loop</li>
<li>Text Message</li>
</ul>
<b>Step 2: </b>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

{% youtube J09RSQVV5xY %}

[Click here to check my configuration in WayScript](https://wayscript.com/shared/l4KwC5XY)

<h4>On one lucky day..</h4>
The price got reduced and this script notified me by sending a text message and I ordered it 😁<br>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/riyf4wqcq35ny4sehh86.jpg)
<br>
<b>If you like my content, please consider supporting me</b><br>
<a href="https://www.buymeacoffee.com/sunilaleti" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-black.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a><br>



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