Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require("http");
  2. var {linkType, get} = require("get-content");
  3. var cheerio = require("cheerio");
  4. http.createServer(function (req, res) {
  5.     var url  = "https://www.lazada.co.id/";
  6.     get(url).then((pageContent) => {
  7.         $ = cheerio.load(pageContent);        
  8.         var result = new Array();
  9.         // var target = $('.c-slider__row','.c-slider__wrapper','.c-slider' ,'.c-mp-section__body','.c-layout-container__section' ,'.c-recommendation__content').find(".c-slider__slide");
  10.         var target = $('.c-recommendation__content').find(".c-slider__slide");
  11.         $(target).each(function(i, elem) {
  12.             temp1  = $('a' ,$(this).html()).html();
  13.            
  14.             img = $('.c-img-lazy' ,temp1).attr("data-js-component-params");
  15.             img = img.split('src\": \"')[1];
  16.             img = img.split('\"\n')[0];            
  17.             result.push({'img':img, 'link':$('a' ,$(this).html()).attr("href"), 'title':$('.c-product-item__title' ,temp1).text().trim(), 'price_new':$('.c-product-item__price' ,temp1).text().replace(/\n/g, "").trim(), 'price_old':$('.c-product-item__price-old' ,temp1).text().trim(), 'discount':$('.c-product-item__discount' ,temp1).text().trim()});
  18.         });
  19.         res.writeHeader(200, {"Content-Type": "text/html"});  
  20.         res.write(JSON.stringify(result));  
  21.         res.end();
  22.     }).catch((err) => {console.warn(err);});
  23. }).listen(8081);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement