Advertisement
Guest User

lowes scraping

a guest
Aug 30th, 2024
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { gotScraping } from "got-scraping";
  2. import * as cheerio from 'cheerio';
  3. import fs from 'graceful-fs';
  4.  
  5. async function getResWithRetry(url, retries = 0) {
  6.   try {
  7.     const res = await gotScraping(url);
  8.  
  9.     if (res.statusCode !== 200) {
  10.       throw new Error(`getProductUrls: Failed to fetch ${res.statusCode}`);
  11.     }
  12.  
  13.     return res.body;
  14.   } catch (error) {
  15.     if (retries < 15) {
  16.       console.log(`RETRYING ${url}`)
  17.       return getResWithRetry(url, retries + 1);
  18.     }
  19.     console.log("error while scrape page", error.message);
  20.   }
  21. }
  22.  
  23. async function getAllProData(proPaths) {
  24.  
  25.   for (let i = 0; i < proPaths.length; i++) {
  26.     const segments = proPaths[i].split('/');
  27.     const proId = segments[segments.length - 1];
  28.  
  29.     const proUrl = `https://www.lowes.com/wpd/${proId}/productdetail/1/Guest`
  30.     const html = await getResWithRetry(proUrl);
  31.  
  32.     const data = JSON.parse(html);
  33.  
  34.     try {
  35.       const priceInfo = data.productDetails[proId].location.price.pricingDataList;
  36.  
  37.       console.log(`product url: https://lowes.com${proPaths[i]}, product id: ${proId}, base price: ${priceInfo[0].basePrice}, final price: ${priceInfo[0].finalPrice}`)
  38.     } catch (error) {
  39.       console.log(error);
  40.     }
  41.  
  42.    
  43.   }
  44. }
  45.  
  46. async function getProductUrls(url) {
  47.   const html = await getResWithRetry(url);
  48.  
  49.   const vb = "window['__PRELOADED_STATE__'] = "
  50.   const $ = cheerio.load(html);
  51.  
  52.   const script = $(`script:contains("${vb}")`).text().slice(vb.length);
  53.   const itemList = JSON.parse(script).itemList;
  54.  
  55.   const proUrls = []
  56.   for (let i = 0; i < itemList.length; i++) {
  57.     proUrls.push(itemList[i].product.pdURL);
  58.   }
  59.  
  60.   return proUrls;
  61. }
  62.  
  63. (async () => {
  64.   const catUrls = [
  65.     // 'https://www.lowes.com/pl/microwaves/4294715798?goToProdList=true',
  66.     'https://www.lowes.com/pl/Labor-day-sale/2110158092346?catalog=4294937007'
  67.     // 'https://www.lowes.com/c/Drills-drivers-Power-tools-Tools'
  68.   ]
  69.  
  70.   const proUrls = await getProductUrls(catUrls[0]);
  71.   const allProData = await getAllProData(proUrls);
  72. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement