-
Use PHP to scrape discount banners from the homepage of Selfridges UK
Scraping discount banners from the Selfridges UK homepage involves using PHP to fetch the webpage content with the Guzzle HTTP client and parse the HTML with DOMDocument and XPath. Discount banners are typically prominent on the homepage, showcasing ongoing promotions, seasonal sales, or limited-time offers. These banners often contain text like “Up to 50% off” or “Limited Time Offer” and are usually located within div elements or sections styled for marketing purposes.
To begin, you inspect the homepage using browser developer tools to identify the exact structure of the discount banner elements. This includes analyzing their tags and classes to build a reliable query for extracting the content. Once identified, the script fetches the HTML content and parses it to locate the relevant sections. Using XPath expressions, you can extract the banner text and any associated links or images. Below is the complete PHP script for scraping discount banners from the Selfridges UK homepage:<?php require 'vendor/autoload.php'; use GuzzleHttp\Client; // Initialize Guzzle client $client = new Client(); $response = $client->get('https://www.selfridges.com/GB/en/homepage'); $html = $response->getBody()->getContents(); // Load HTML into DOMDocument $dom = new DOMDocument; libxml_use_internal_errors(true); $dom->loadHTML($html); libxml_clear_errors(); // Initialize XPath $xpath = new DOMXPath($dom); // Scrape discount banners $banners = $xpath->query('//div[contains(@class, "banner-discount")]'); if ($banners->length > 0) { foreach ($banners as $banner) { $text = trim($banner->nodeValue); $link = $banner->getElementsByTagName('a')->item(0)->getAttribute('href') ?? 'No link'; echo "Banner: $text\n"; echo "Link: $link\n"; echo "-------------------\n"; } } else { echo "No discount banners found.\n"; } ?>
Log in to reply.