Developing Amazon price checker in Javascript
- Get link
- X
- Other Apps
Amazon price checker in Javascript using Nightmare
Disclaimer: This post is for educational purposes only. We do not encourage or promote any sort of web scraping.
Objective:
Have you ever thought of checking the price of your favorite product on any e-commerce site using your code. In this post we are going to achieve that. We will check the price of our selected product on amazon with the help of Nightmare module.
Refer the link for more details on Nightmare module : https://www.npmjs.com/package/nightmare
Solution Design:
let's proceed with our very first step, i.e to shortlist the product and get the Amazon URL. For this project we have selected the "OnePlus 8" with the URL : https://www.amazon.in/Test-Exclusive-547/dp/B078BNQ318/
Step 2:
We will get the element for the price of the product from the amazon URL. For this open the developer tools in the browser and inspect the price element. For reference, check the below screenshot.
Step 3:
We have selected "priceblock_ourprice" as the price element. Similarly we can fetch any other element that we wish to collect from the amazon page. Just for example we have captured "productTitle" for the product title as well.
Step 4:
Install the "Nightmare" module with the command:
npm i nightmare
we will require that in our code :
Step 5 : Let's move to the coding part now:
const nightmare = require('nightmare')()
const args = process.argv.slice(2) // fetch the URL of the product from the node process arguments
const url = args[0]
checkPrice()
async function checkPrice() {
try {
const resultString = await nightmare.goto(url)
.wait("#productTitle")
.evaluate(() =>document.getElementById("productTitle").innerText+" price:"+document.getElementById("priceblock_ourprice").innerText )
.end()
console.log("Name : "+ resultString)
} catch (e) {
throw e;
}
}
Step 6: To run the code and pass the URL use below command:
node amazon-mytracker.js https://www.amazon.in/Test-Exclusive-547/dp/B078BNQ318/
For testing, i have tested the code with two different input (product URLs).
Below screenshot shows the input and output of our code:
In the code we have used only the product title and product price information in the response, the code can be customized as per the requirement.
In the next part, we will add the functionality of cron job and email to convert this price checker into Amazon price tracker.
- Get link
- X
- Other Apps
Comments
Astounding modern solution to modern era problem. ❣️
ReplyDelete