Forum Replies Created

  • To scrape ratings, locate the element displaying customer ratings, often in a span or div tag. Use DOMXPath to select and extract the rating value.

    <?php
    require 'vendor/autoload.php';
    use GuzzleHttp\Client;
    // Initialize Guzzle client
    $client = new Client();
    $response = $client->get('https://www.asos.com/product-page');
    $html = $response->getBody()->getContents();
    // Load HTML into DOMDocument
    $dom = new DOMDocument;
    libxml_use_internal_errors(true);
    $dom->loadHTML($html);
    libxml_clear_errors();
    // Extract product ratings
    $xpath = new DOMXPath($dom);
    $rating = $xpath->query('//span[@class="product-rating"]')->item(0)->nodeValue;
    echo "Rating: $rating\n";
    ?>
    
  • To make the script more robust, you can include a retry mechanism for failed requests. Colly provides built-in support for retries, which is useful for handling temporary network issues or server-side rate-limiting.