I'm scraping data from SamsClub.com using PHP
$res = file_get_contents('http://www.samsclub.com/sams/bath-towel-apple-gr-100-cotton/prod10450797.ip');
I have create function using PHP explode to fetch data.
function getData($content,$start,$end){
$str = explode($start,$content);
$str = explode($end,$str[1]);
return $str[0];
}
All required data fetched successfully but only one thing is remaining. That is variations of product mean other colors as u can see in snapshot, there are different colors available.
When we select an other color the item # & Model # of product also changed as shown in following snapshot
I just want to also fetch information like "item # & model #" of other colors too.
Waiting for your great response guys.
to do this you will need to use a library (PHP Simple HTML DOM Parser). Just upload simple_html_dom.php somewhere were you are able to include it (in my code, it's in the same folder).
<?php
$url = 'http://www.samsclub.com/sams/bath-towel-apple-gr-100-cotton/prod10450797.ip';
include('simple_html_dom.php');
$html = file_get_html($url);
$colour = array(); $item = array(); $model = array();
$script = $html->find('div[id=variance] script', 0)->innertext;
$script = preg_replace('/\s+/', ' ', $script);
$scripts = explode (";", $script);
$script = $scripts[2];
$id = $scripts[4];
$type = $scripts[5];
$script = str_replace("skuJson.skuVariantJson = $.parseJSON('", "", $script);
$script = str_replace("')", "", $script);
$colours = json_decode($script);
preg_match("/'([a-z0-9]*)'/", $type, $types); $type = $types[1];
preg_match("/'([a-z0-9]*)'/", $id, $ids); $id = $ids[1];
$script = $html->find('script', -1)->innertext;
$scripts = explode (";", $script);
$time = $scripts[0];
preg_match('/"([0-9]*)"/', $time, $times); $time = $times[1];
foreach ($colours as $key => $value) {
$url = 'http://www.samsclub.com/sams/shop/product/ajax/ajaxSkuVariant.jsp?skuId='. $value .'&productId='. $id .'&productType='. $type .'&_='. $time;
$html = file_get_html($url);
preg_match('/"legacyItemNumber":"([0-9]*)"/', $html, $match); $item[] = $match[1];
preg_match('/"model":"([a-z-]*)"/i', $html, $match); $model[] = $match[1];
$colour[] = substr($key, 0, -1);
}
//Print results
echo "<pre>"; print_r($colour); echo "</pre>";
echo "<pre>"; print_r($item); echo "</pre>";
echo "<pre>"; print_r($model); echo "</pre>";
?>
The only thing you need to change is the $url variable at the beginning. Why all this code, you may ask ... because the data you are looking for isn't on the same page and it's called via ajax every time you click on a colour, so basically we are making a lot of requests (one for each color). This is the output:
Array
(
[0] => White
[1] => Burgundy
[2] => Apple Green
[3] => Lilac
[4] => Chocolate
[5] => Sage
[6] => Grey
[7] => PckBlue
[8] => Linen
[9] => null
[10] => Plum
[11] => Clay
[12] => Light Blue
)
Array
(
[0] => 252368
[1] => 252505
[2] => 252414
[3] => 433076
[4] => 252389
[5] => 117268
[6] => 252438
[7] => 613317
[8] => 252382
[9] => 433083
[10] => 252541
[11] => 117175
[12] => 252400
)
Array
(
[0] => SAMW-B
[1] => SAMB-B
[2] => SAMA-B
[3] => SAMLC-B
[4] => SAMCH-B
[5] => SAMSS-B
[6] => SAMGR-B
[7] => SAMPB-B
[8] => SAMLI-B
[9] => SAMDR-B
[10] => SAMP-B
[11] => SAMTC-B
[12] => SAMLB-B
)