I need to scrap the following elements inside each one of these div's class="product-grid-item" (page contains several of them), but in fact I have no clue how to do it... so, I need help not to pull my hair out.
1 - The link and image inside the div: class="product-element-top2;
<a href="https://...this_link" class="product-image-link"> (just need the link)
<img width="300" height="300" src="https://...this_image_url... (just need this image URL)
2 - The title inside the h3 tag as follows;
<h3 class="wd-entities-title"><a href="https://...linkhere">The title goes here (just the title)
3 - Last but not least, I need to grab tha price inside this;
<span class="price"><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">€</span>20,00</bdi></span></span> (just the "€20.00")
Here's the full HTML:
<div class="product-grid-item" data-loop="1">
<div class="product-element-top">
<a href="https://...linkhere" class="product-image-link">
<img width="300" height="300" src="https://image-goes-here.jpg" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail"> </a>
<div class="top-information wd-fill">
<h3 class="wd-entities-title"><a href="https://...linkhere">The title goes here</a></h3>
<span class="price"><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">€</span>20,00</bdi></span></span>
<div class="wd-add-btn wd-add-btn-replace woodmart-add-btn">
<a href="https://...linkhere" data-quantity="1" class="button product_type_variable add_to_cart_button add-to-cart-loop"><span>Options</span></a></div>
</div>
<div class="wd-buttons wd-pos-r-t color-scheme-light woodmart-buttons">
<div class="wd-compare-btn product-compare-button wd-action-btn wd-style-icon wd-compare-icon">
<a href="https://...linkhere" data-added-text="Compare Products">Buy</a>
</div>
<div class="quick-view wd-action-btn wd-style-icon wd-quick-view-icon wd-quick-view-btn">
<a href="https://...linkhere" class="open-quick-view quick-view-button">quick view</a>
</div>
<div class="wd-wishlist-btn wd-action-btn wd-style-icon wd-wishlist-icon woodmart-wishlist-btn">
<a class="" href="https://linkhere/wishlist/" data-key="dcf36756534755" data-product-id="387654" data-added-text="See Wishlist">Wishlist</a>
</div>
</div>
<div class="quick-shop-wrapper wd-fill wd-scroll">
<div class="quick-shop-close wd-action-btn wd-style-text wd-cross-icon"><a href="#" rel="nofollow noopener">Close</a></div>
<div class="quick-shop-form wd-scroll-content">
</div>
</div>
</div>
</div>
One of my clumsy attempts:
$html = file_get_contents("https://url-here.goetohere");
$DOM = new DOMDocument();
$DOM->loadHTML($html);
$finder = new DomXPath($DOM);
$classname = 'product-grid-item';
$classname = 'product-element-top2';
$classname = 'product-element-top2';
$classname = 'wd-entities-title';
$classname = 'price';
$nodes = $finder->query("//*[contains(@class, '$classname')]");
foreach ($nodes as $node) {
echo 'here »» ' . htmlentities($node->nodeValue) . '<br>';
}