Advertisement
dereksir

Untitled

Mar 7th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // import required libraries
  2. const axios = require('axios');
  3. const cheerio = require('cheerio');
  4.  
  5. // define the URLs to scrape
  6. const urls = ['http://example.com/1', 'https://example.com/2', 'https://example.com/3']; // replace with actual URLs
  7.  
  8. const scraper = async (urls) => {
  9.   try {
  10.     // loop through each URL
  11.     for (let i = 0; i < urls.length; i++) {
  12.         // make an HTTP GET request to the current URL
  13.         const response = await axios.get(urls[i]);
  14.        
  15.         // load HTML content into Cheerio
  16.         const $ = cheerio.load(response.data);
  17.  
  18.         // perform parsing operations
  19.         // example:
  20.         console.log(`Response for URL ${i + 1}:`, $('body').text());
  21.  
  22.         // wait for 5 seconds before making the next request
  23.         await new Promise(resolve => setTimeout(resolve, 5000));
  24.     }
  25.   } catch (error) {
  26.     console.error('Error:', error);
  27.   }
  28. };
  29.  
  30. scraper(urls);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement