Advertisement
Guest User

Untitled

a guest
Feb 6th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const nightmare = require('nightmare')({ show: false });
  2. const cheerio = require('cheerio');
  3.  
  4. module.exports.parseData = async (page) => {
  5.   const url = `http://otkos-profi.ru/product${ page }.html`;
  6.  
  7.   const html = await nightmare
  8.   .goto(url)
  9.   .wait('body')
  10.   .evaluate(() => {
  11.     return document.querySelector('body').innerHTML;
  12.   })
  13.   .end();
  14.  
  15.   const parsedData = await getData(html);
  16.   return parsedData;
  17. };
  18.  
  19. const getData = async (html) => {
  20.   const $ = cheerio.load(html);
  21.  
  22.   const title = await $('div.path').children('h1').text();
  23.   const description = await $('div.col-md-12').text();
  24.   const price = await $('div.product-info-box').children('div.price').children('span').text();
  25.   const unit = await $('div.product-info-box').children('div.price').children('sub').text();
  26.   const categories = getCategory($);
  27.   const images = getImages($);
  28.  
  29.   return {
  30.     title,
  31.     description,
  32.     price,
  33.     unit,
  34.     categories,
  35.     images
  36.   };
  37. };
  38.  
  39. const getCategory = ($) => {
  40.   let categories = [];
  41.   const category = $('ol.breadcrumb').children('li').each(async (i, element) => {
  42.     if (i !== 0 && i !== 1)
  43.      await categories.push($(element).text());
  44.       //categoriesString += `${  } > `;
  45.   });
  46.  
  47.   return categories;
  48. };
  49.  
  50. const getImages = ($) => {
  51.   let images = [];
  52.   const image = $('a.gallery').each(async (i, element) => { //;
  53.     await images.push($(element).attr('data-image'));
  54.   });
  55.  
  56.   return images;
  57. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement