Advertisement
az26

Untitled

Feb 13th, 2025
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const puppeteer = require('puppeteer-extra');
  2. const StealthPlugin = require('puppeteer-extra-plugin-stealth');
  3. const fs = require('fs'); // Import the fs module to work with files
  4.  
  5. puppeteer.use(StealthPlugin());
  6.  
  7. (async () => {
  8.     const browser = await puppeteer.launch({
  9.             executablePath:'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
  10.         headless: false,
  11.         args: [
  12.             "--no-sandbox",
  13.             "--disable-setuid-sandbox",
  14.             "--disable-blink-features=AutomationControlled"
  15.         ]
  16.     });
  17.  
  18.     const page = await browser.newPage();
  19.     await page.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36");
  20.  
  21.     let allData = [];
  22.  
  23.     for (let listPage = 1; listPage <= 5; listPage++) {
  24.         console.log(`Fetching data from listpage=${listPage}...`);
  25.        
  26.         // Construct the URL with the changing listpage parameter
  27.         const apiUrl = `https://easy.co.il/n/jsons/bizlist?version=2.3&c=268&client=web&listpage=${listPage}&lat=32.059925&lng=34.785126&rad=8905&mapid=0&viewport=desktop&lang=he`;
  28.        
  29.         // Navigate to the page
  30.         await page.goto("https://easy.co.il/list/Hair-Design", { waitUntil: 'networkidle2' });
  31.  
  32.         // Wait for the page to load completely
  33.         await new Promise(resolve => setTimeout(resolve, 3000));
  34.  
  35.         // Extract data from the API response
  36.         const data = await page.evaluate((listPage) => {
  37.             return fetch(`https://easy.co.il/n/jsons/bizlist?version=2.3&c=268&client=web&listpage=${listPage}&lat=32.059925&lng=34.785126&rad=8905&mapid=0&viewport=desktop&lang=he`)
  38.                 .then(response => response.json())
  39.                 .then(json => json.bizlist?.list || []) // Extract only the list
  40.                 .catch(err => console.log("Error fetching data:", err));
  41.         });
  42.  
  43.         allData.push(...data); // Append the results to the array
  44.  
  45.         console.log(`✅ Retrieved ${data.length} businesses from page ${listPage}`);
  46.  
  47.         // Wait before making the next request to avoid being blocked
  48.     }
  49.  
  50.     console.log(`🔹 Total businesses collected: ${allData.length}`);
  51.  
  52.     // Extract only required fields
  53.     const formattedData = allData.map(biz => ({
  54.         id: biz.id,
  55.         city: biz.city,
  56.         category: biz.category,
  57.         bizname: biz.bizname,
  58.         address: biz.address,
  59.         phone: biz.phone
  60.     }));
  61.  
  62.     // Write the formatted data to a file
  63.     const filePath = 'formattedData.json';
  64.     fs.writeFileSync(filePath, JSON.stringify(formattedData, null, 2), 'utf-8'); // Save as a JSON file
  65.     console.log(`📌 Data saved to ${filePath}`);
  66.     console.log(JSON.stringify(formattedData, null, 2));
  67.  
  68.     await browser.close();
  69. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement