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