mardi 16 avril 2019

Web Scraping with cURL in Walmart not work

I'm trying to get the price of a product in different pages, the following code works perfect on many pages, even on Amazon, but I can not get it to work at Walmart.com.mx

Code that works in Amazon and many other pages:

<?php
$walmart = "https://www.walmart.com.mx/tv-y-video/pantallas/todas/tv-hisense-55-pulgadas-4k-ultra-hd-smart-tv-led-55h6e_00694214743951";

function Walmart($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$data = Walmart($walmart);

preg_match("'<h4 class=\"_14IH4aAI74yIT_AVmcwOAY header _3uTBp1K6mn9jSKxCD9miQD\" itemprop=\"price\">(.*?)</h4>'", $data, $price);

echo $price[1];
?>

Also try the following code for Walmart.com.mx but it does not work either:

<?php
$walmart = "https://www.walmart.com.mx/tv-y-video/pantallas/todas/tv-hisense-55-pulgadas-4k-ultra-hd-smart-tv-led-55h6e_00694214743951";

function Walmart($url) {
    $agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_VERBOSE, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$data = Walmart($walmart);

preg_match("'<h4 class=\"_14IH4aAI74yIT_AVmcwOAY header _3uTBp1K6mn9jSKxCD9miQD\" itemprop=\"price\">(.*?)</h4>'", $data, $price);

echo $price[1];
?>

Any idea of how I should access the price of the product on this specific page? Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire