News Feed Forums General Web Scraping How to scrape product prices from Sainsburys.co.uk using JavaScript?

  • How to scrape product prices from Sainsburys.co.uk using JavaScript?

    Posted by Olwen Haider on 12/21/2024 at 11:33 am

    Scraping product prices from Sainsburys.co.uk using JavaScript is an effective way to gather information about grocery items, including pricing trends, discounts, and availability. Sainsbury’s is one of the largest supermarket chains in the UK, making it a valuable source for analyzing market data. Using Node.js and Puppeteer, you can automate interactions with the website and extract specific details like product names, prices, and promotional tags. The process starts with inspecting the webpage to identify the structure of the HTML and locating the elements containing the desired data. By navigating to the grocery section, you can collect detailed information about various categories and products.
    One of the complexities of scraping Sainsburys.co.uk is dealing with dynamic content. Many product pages rely on JavaScript to load data, making Puppeteer an ideal tool to simulate browser behavior. Incorporating logic to handle filtering and sorting on the website can enhance the scraper’s functionality. For example, extracting organic products or special dietary items adds depth to the collected data. Below is an example script that demonstrates how to collect product names and prices from Sainsburys.co.uk.

    const puppeteer = require('puppeteer');
    (async () => {
        const browser = await puppeteer.launch({ headless: true });
        const page = await browser.newPage();
        const url = 'https://www.sainsburys.co.uk/';
        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('.price')?.textContent.trim() || 'Price not available';
                productList.push({ name, price });
            });
            return productList;
        });
        console.log(products);
        await browser.close();
    })();
    

    This script navigates to the Sainsbury’s website, extracts product names and prices, and logs the data. Expanding the script to capture promotional tags or product categories can add further value. Adding logic to retry failed requests ensures smoother execution and reduces potential data gaps.

    Olwen Haider replied 5 hours, 31 minutes ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

Log in to reply.