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