Skip to content

Commit 0b84ada

Browse files
authored
Merge pull request #291 from Ninjavin/amazon-scrape-nodejs
Amazon Scraping Using Nodejs Added
2 parents 1fad716 + efd33ab commit 0b84ada

File tree

6 files changed

+493
-0
lines changed

6 files changed

+493
-0
lines changed

JavaScript/Amazon-Scrape/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

JavaScript/Amazon-Scrape/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Scrape Amazon Using NodeJS
2+
3+
This script scrapes Amazon for the product you searched and returns the top 5 results with their price and ratings
4+
5+
## How to Run?
6+
+ Run `npm i` to install the dependencies.
7+
+ Run `node index.js "<product-name>"` to search for a particular product.
8+
9+
## Example
10+
11+
![image](images/Amazon-Scrape.png)
Loading

JavaScript/Amazon-Scrape/index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// To Scrape the Amazon Website
2+
const puppeteer = require('puppeteer');
3+
// To print table in the output
4+
const Table = require('cli-table');
5+
const table = new Table({
6+
head: ['Product Name', 'Price', 'Ratings'],
7+
colWidths: [140, 10, 10]
8+
})
9+
// To get user arguments
10+
const args = process.argv;
11+
const amazon = (async () => {
12+
// Launching the browser
13+
const browser = await puppeteer.launch({ timeout: 0 })
14+
const page = await browser.newPage()
15+
await page.goto("https://www.amazon.com/", { timeout: 0, waitUntil: 'domcontentloaded' })
16+
// Getting the User Input
17+
const name = args[2]
18+
// Selecting the Search Box
19+
const elementHandle = await page.$('input#twotabsearchtextbox.nav-input')
20+
// Typing in the user argument in the search box
21+
await elementHandle.type(name)
22+
// Pressing Enter
23+
await elementHandle.press('Enter')
24+
await page.waitForNavigation({waitUntil: "domcontentloaded"})
25+
const results = await page.evaluate(() => {
26+
const searchresult = Array.from(document.querySelectorAll('.s-result-item.s-asin'))
27+
return searchresult.map(r => {
28+
if (r.querySelector(".a-price")) {
29+
return {
30+
product: r.querySelector(".a-color-base.a-text-normal").textContent,
31+
price: (r.querySelector(".a-price").textContent),
32+
ratings: r.querySelector(".a-icon-alt") ? r.querySelector(".a-icon-alt").textContent : "NA"
33+
};
34+
}
35+
}).slice(0,5)
36+
})
37+
// Printing the results
38+
console.log("\nHere are the top 5 products based on your search: \n")
39+
for(let i=0 ; i<5 ; i++){
40+
table.push(
41+
[`${results[i].product}`, `${results[i].price}`, `${results[i].ratings.slice(0, 3)}`]
42+
)
43+
}
44+
console.log(table.toString())
45+
await browser.close()
46+
})
47+
// Calling the function
48+
if(args.length === 2)
49+
console.log('Correct Command Usage: `node index.js "product-name"`')
50+
else
51+
amazon()

0 commit comments

Comments
 (0)