1+ const Nightmare = require ( 'nightmare' )
2+ const cheerio = require ( 'cheerio' ) ;
3+ const nightmare = Nightmare ( { show : true } ) ;
4+ const url = 'https://www.flipkart.com/' ;
5+ 6+ nightmare
7+ . goto ( url )
8+ . wait ( 'body' )
9+ . click ( 'button._2AkmmA._29YdH8' )
10+ . type ( 'input.LM6RPg' , 'laptop' )
11+ . click ( 'button.vh79eN' )
12+ . wait ( 'div.bhgxx2' )
13+ . evaluate ( ( ) => document . querySelector ( 'body' ) . innerHTML )
14+ . end ( )
15+ . then ( response => {
16+ console . log ( getData ( response ) ) ;
17+ } ) . catch ( err => {
18+ console . log ( err ) ;
19+ } ) ;
20+ 21+ let getData = html => {
22+ data = [ ] ;
23+ const $ = cheerio . load ( html ) ;
24+ $ ( 'div._1HmYoV._35HD7C:nth-child(2) div.bhgxx2.col-12-12' ) . each ( ( row , raw_element ) => {
25+ $ ( raw_element ) . find ( 'div div div' ) . each ( ( i , elem ) => {
26+ let title = $ ( elem ) . find ( 'div div a:nth-child(2)' ) . text ( ) ;
27+ let link = $ ( elem ) . find ( 'div div a:nth-child(2)' ) . attr ( 'href' ) ;
28+ if ( title ) {
29+ data . push ( {
30+ title : title ,
31+ link : link
32+ } ) ;
33+ }
34+ } ) ;
35+ } ) ;
36+ return data ;
37+ }
0 commit comments