Forum Replies Created

  • To determine delivery options, identify the section of the page that lists available shipping methods and their respective details. By querying this section, you can extract information such as delivery times and costs.

    <?php
    // Initialize cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.carrefour.com.br/produto-page');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $html = curl_exec($ch);
    curl_close($ch);
    // Load HTML into DOMDocument
    $dom = new DOMDocument;
    libxml_use_internal_errors(true);
    $dom->loadHTML($html);
    libxml_clear_errors();
    // Create XPath to navigate DOM
    $xpath = new DOMXPath($dom);
    // Extract delivery options
    $options = $xpath->query('//div[@class="delivery-options"]//li');
    foreach ($options as $option) {
        echo trim($option->nodeValue) . "\n";
    }
    ?>
    
  • To obtain the price, locate the element containing the pricing information, which is often within a span or div tag with a class indicating price. Using Nokogiri, you can select this element and extract the text, ensuring you handle any currency symbols appropriately.

    require 'net/http'
    require 'nokogiri'
    # Fetch the product page
    url = URI('https://www.magazineluiza.com.br/produto-page')
    response = Net::HTTP.get(url)
    # Parse the HTML
    doc = Nokogiri::HTML(response)
    # Extract the product price
    price = doc.at_css('.product-price').text.strip
    puts "Price: #{price}"