robertohozza

Untitled

Mar 26th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. ( async() =>
  4. {
  5.     const request = require('request');
  6.     const cheerio = require('cheerio');
  7.  
  8.     let categories = [];
  9.  
  10.     await new Promise( resolve =>
  11.     {
  12.         request('https://goldex.heureka.cz/kategorie/', async(err, resp, body) =>
  13.         {
  14.             let dom = cheerio.load(body);
  15.  
  16.             dom('div.brand-categories a').each( ( i, e ) => categories.push(e.attribs.href) );
  17.  
  18.             resolve();
  19.         });
  20.     });
  21.  
  22.     let products = {};
  23.  
  24.     for( let category of categories )
  25.     {
  26.         let offset = 0;
  27.  
  28.         console.log( category );
  29.  
  30.         while( true )
  31.         {
  32.             let productsCnt = await new Promise( resolve =>
  33.             {
  34.                 request( category + '?f=' + offset, async(err, resp, body) =>
  35.                 {
  36.                     let dom = cheerio.load(body);
  37.                     let pruductURLs = [];
  38.  
  39.                     dom('div.image a').each( ( i, e ) => pruductURLs.push(e.attribs.href) );
  40.                       console.log(pruductURLs)
  41.                     for( let productURL of pruductURLs )
  42.                     {
  43.                         await new Promise( resolve =>
  44.                         {
  45.                             request( productURL, async(err, resp, body) =>
  46.                             {
  47.                                 let dom = cheerio.load(body);
  48.                                 let categories =[];
  49.                                   dom('span.js-gtm-breadcrumbs-item a').map(function(i, el) {
  50.                                   categories.push(dom(this).text());
  51.                                 })
  52.                                 let prodnamimg = {};
  53.                                 let name =  dom('div.text-cover H1').text();
  54.                                 let prices =  dom('span.js-top-price').text();
  55.                             /*     dom('a.sigProLink', 'div.itemImageGallery').each( ( i, e ) => images.push('http://www.orava.eu'+e.attribs.href));
  56.  
  57.                                 if( !images.length && dom('a.modal').length )
  58.                                 {
  59.                                     images.push('http://www.orava.eu'+ dom('a.modal').first()['0'].attribs.href );
  60.                                 }
  61.                                   */
  62.                                 products[name] = { category: categories.join(' > '), price: prices };
  63.  
  64.                                 resolve( products );
  65.                             });
  66.  
  67.                         });
  68.                     }
  69.                     resolve( pruductURLs.length );
  70.                 });
  71.             });
  72.  
  73.             if( productsCnt < 60 ){ break; }
  74.  
  75.             offset += 1;
  76.         }
  77.     }
  78.  
  79.     require('fs').writeFileSync('export.json', JSON.stringify(products), function (err) {
  80.     if (err) {
  81.         return console.log(err);
  82.     }
  83.  
  84.     console.log("The file was saved!");
  85. });
  86. })();
Advertisement
Add Comment
Please, Sign In to add comment