Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fetch = require('node-fetch');
  2. const url = 'https://www.paris.cl/search?q='
  3. const cheerio = require('cheerio');
  4.  
  5. function searchProducts(searchTerm) {
  6.     return fetch(`${url}${searchTerm}`)
  7.         .then(response => response.text())
  8.         .then(body => {
  9.             const products = [];
  10.             const $ = cheerio.load(body);
  11.             $('.product-tile').each(function(i, element){
  12.                 const $element = $(element);
  13.                 //const $image =$element.find('a img');
  14.                 const $title =$element.find('h4');
  15.                 const $price =$element.find('.price');
  16.                 const product = {
  17.                 //    image: $image.attr('src'),
  18.                     title: $title.text().replace(/[\n\r]/g,' '),
  19.                     Price: $price.text().replace(/[\n\r]/g,' '),
  20.                 };
  21.                 products.push(product);
  22.             });
  23.             return products
  24.         });
  25.     }
  26.     module.exports = {
  27.         searchProducts
  28.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement