-
What data can I scrape from Nordstrom.com for product reviews?
Scraping product reviews from Nordstrom.com can provide insights into customer opinions, ratings, and feedback on various items. Using PHP, you can send HTTP requests to retrieve web pages and parse their HTML content to extract relevant data. By analyzing the structure of the product review section, you can identify tags or elements containing review details such as customer names, ratings, and review text. This process involves initializing a request to the desired product page, loading the content into a parser, and extracting the required fields. Below is an example of how to scrape reviews from Nordstrom.com using PHP.
<?php $url = "https://www.nordstrom.com/s/womens-shoes"; $options = [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_USERAGENT => "Mozilla/5.0" ]; $ch = curl_init(); curl_setopt_array($ch, $options); $html = curl_exec($ch); curl_close($ch); $dom = new DOMDocument(); libxml_use_internal_errors(true); $dom->loadHTML($html); libxml_clear_errors(); $xpath = new DOMXPath($dom); $products = $xpath->query("//div[contains(@class, 'product-card')]"); foreach ($products as $product) { $name = $xpath->query(".//span[contains(@class, 'product-title')]", $product)->item(0)->nodeValue ?? "Name not available"; $price = $xpath->query(".//span[contains(@class, 'product-price')]", $product)->item(0)->nodeValue ?? "Price not available"; $rating = $xpath->query(".//span[contains(@class, 'rating')]", $product)->item(0)->nodeValue ?? "No rating available"; echo "Name: $name, Price: $price, Rating: $rating\n"; } ?>
This PHP script uses cURL to fetch the product page and DOMDocument with XPath to parse and extract product details. The script targets product titles, prices, and ratings, ensuring default values are provided for missing elements. To handle pagination, you can modify the script to identify and navigate to additional pages. Incorporating error handling ensures the scraper continues to function smoothly even if the page structure changes.
Log in to reply.