News Feed Forums General Web Scraping How to scrape product details from Petco.com using JavaScript?

  • How to scrape product details from Petco.com using JavaScript?

    Posted by Egzona Zawisza on 12/20/2024 at 11:09 am

    Scraping product details from Petco.com using JavaScript allows you to collect data such as product names, prices, and availability. Using Node.js with Puppeteer, you can automate browser interactions to handle dynamic content and extract the necessary information. Below is a sample script for scraping product data from Petco.

    const puppeteer = require('puppeteer');
    (async () => {
        const browser = await puppeteer.launch({ headless: true });
        const page = await browser.newPage();
        const url = 'https://www.petco.com/shop/en/petcostore/category/dog';
        await page.goto(url, { waitUntil: 'networkidle2' });
        const products = await page.evaluate(() => {
            const productList = [];
            const items = document.querySelectorAll('.product-card');
            items.forEach(item => {
                const name = item.querySelector('.product-name')?.textContent.trim() || 'Name not available';
                const price = item.querySelector('.product-price')?.textContent.trim() || 'Price not available';
                const availability = item.querySelector('.availability')?.textContent.trim() || 'Availability not available';
                productList.push({ name, price, availability });
            });
            return productList;
        });
        console.log(products);
        await browser.close();
    })();
    

    This script navigates to Petco’s dog category page, waits for the content to load, and extracts product names, prices, and availability. Pagination can be added to scrape additional product listings by automating navigation through “Next” buttons. Randomizing request timing helps avoid detection.

    Egzona Zawisza replied 2 days, 8 hours ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

Log in to reply.