Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const nightmare = require('nightmare')({ show: false });
- const cheerio = require('cheerio');
- module.exports.parseData = async (page) => {
- const url = `http://otkos-profi.ru/product${ page }.html`;
- const html = await nightmare
- .goto(url)
- .wait('body')
- .evaluate(() => {
- return document.querySelector('body').innerHTML;
- })
- .end();
- const parsedData = await getData(html);
- return parsedData;
- };
- const getData = async (html) => {
- const $ = cheerio.load(html);
- const title = await $('div.path').children('h1').text();
- const description = await $('div.col-md-12').text();
- const price = await $('div.product-info-box').children('div.price').children('span').text();
- const unit = await $('div.product-info-box').children('div.price').children('sub').text();
- const categories = getCategory($);
- const images = getImages($);
- return {
- title,
- description,
- price,
- unit,
- categories,
- images
- };
- };
- const getCategory = ($) => {
- let categories = [];
- const category = $('ol.breadcrumb').children('li').each(async (i, element) => {
- if (i !== 0 && i !== 1)
- await categories.push($(element).text());
- //categoriesString += `${ } > `;
- });
- return categories;
- };
- const getImages = ($) => {
- let images = [];
- const image = $('a.gallery').each(async (i, element) => { //;
- await images.push($(element).attr('data-image'));
- });
- return images;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement