News Feed Forums General Web Scraping How to scrape product availability from Aldi.co.uk using Go?

  • How to scrape product availability from Aldi.co.uk using Go?

    Posted by Medine Daniyal on 12/21/2024 at 11:19 am

    Scraping product availability from Aldi.co.uk using Go provides a way to track stock levels, product names, and prices for a wide range of items, including groceries and household essentials. Aldi is a leading supermarket chain, and its website features detailed information about in-store and online products. Using Go, you can send HTTP requests to the website and parse the response to identify data points like availability and pricing. This approach ensures that you can track inventory changes and pricing trends efficiently.
    Handling dynamically loaded content can be a challenge, especially for products with fluctuating availability. Introducing logic to refresh and recheck availability ensures accurate data collection. Below is an example Go script for scraping product availability from Aldi.co.uk.

    package main
    import (
    	"fmt"
    	"net/http"
    	"golang.org/x/net/html"
    )
    func main() {
    	url := "https://www.aldi.co.uk/"
    	resp, err := http.Get(url)
    	if err != nil {
    		fmt.Println("Failed to fetch the page")
    		return
    	}
    	defer resp.Body.Close()
    	doc, err := html.Parse(resp.Body)
    	if err != nil {
    		fmt.Println("Failed to parse HTML")
    		return
    	}
    	var parse func(*html.Node)
    	parse = func(node *html.Node) {
    		if node.Type == html.ElementNode && node.Data == "div" {
    			for _, attr := range node.Attr {
    				if attr.Key == "class" && attr.Val == "product-card" {
    					fmt.Println("Product found")
    				}
    			}
    		}
    		for child := node.FirstChild; child != nil; child = child.NextSibling {
    			parse(child)
    		}
    	}
    	parse(doc)
    }
    

    This script identifies product cards on Aldi.co.uk’s product pages. Additional functionality can be added to extract prices, availability statuses, and product descriptions. Handling dynamic content ensures that all relevant data is captured accurately.

    Medine Daniyal replied 19 hours, 31 minutes ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

Log in to reply.