Advertisement
Hannaichi

Protergo Exercise #1 by Rama

Aug 28th, 2023
1,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const axios = require('axios');
  2. const cheerio = require('cheerio');
  3. const fs = require('fs');
  4.  
  5. const websiteUrls = [
  6.   'https://example.com/website1',
  7.   'https://example.com/website2',
  8.   // Add more URLs as needed
  9. ];
  10.  
  11. // Function to scrape product information from a website
  12. async function scrapeWebsite(url) {
  13.   try {
  14.     const response = await axios.get(url);
  15.     const $ = cheerio.load(response.data);
  16.  
  17.     // Extract product information using CSS selectors
  18.     const products = [];
  19.     $('.product').each((index, element) => {
  20.       const name = $(element).find('.product-name').text().trim();
  21.       const price = $(element).find('.product-price').text().trim();
  22.       const details = $(element).find('.product-details').text().trim();
  23.  
  24.       products.push({ name, price, details });
  25.     });
  26.  
  27.     return products;
  28.   } catch (error) {
  29.     console.error(`Error scraping ${url}: ${error.message}`);
  30.     return [];
  31.   }
  32. }
  33.  
  34. // Function to scrape all websites and save the data in a structured way
  35. async function scrapeWebsites() {
  36.   try {
  37.     const allProducts = [];
  38.  
  39.     for (const url of websiteUrls) {
  40.       const products = await scrapeWebsite(url);
  41.       allProducts.push(...products);
  42.     }
  43.  
  44.     // Save the data as JSON
  45.     const data = JSON.stringify(allProducts, null, 2);
  46.     fs.writeFileSync('product_data.json', data);
  47.  
  48.     console.log('Scraping completed successfully!');
  49.   } catch (error) {
  50.     console.error('Error scraping websites:', error);
  51.   }
  52. }
  53.  
  54. // Start the scraping process
  55. scrapeWebsites();
  56.  
Tags: JavaScript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement