The goal's of the project are:
- Learn how to scrape ecommerce data to Magento's configurable product import format
- Get some sexy Magento sample store data for use in future testing and mock-ups
Let's go over some of the code. First we instantiate our CSV object (yes, it's a global variable. I'm okay with that.) Then we load the listings page and iterate through each listing. Pretty self explanatory so far.
$csv = new CSV('products.csv', $fields, ",", null); // no utf-8 BOM
// and start scraping
$url = 'http://www.spicylingerie.com/';
$page = $browser->get($url);
foreach($page->search('//div[@class="fp-pro-name"]/a') as $a){
scrape($a);
echo '.';
}
So now we pass the
a elements that have the details page urls to our scrape function. Because we earlier did $browser->convertUrls = true we no longer need to worry about converting our relative hrefs to absolute urls. The library took care of that for us.
Now we get the page for the link and start building our $item array which we will pass to the save() function. Other than the ugly expression for description this was easy.
$url = $a->getAttribute('href');
$page = $browser->get($url);
$item = array();
$item['name'] = trim($a->nodeValue);
$item['description'] = $item['short_description'] = trim($page->at('//div[@class="pro-det-head"]/h4/text()[normalize-space()][position()=last()]')->nodeValue);
if(!preg_match('/Sale price: \$(\d+\.\d{2})/', $page->body, $m)) die('missing price!');
$item['price'] = $m[1];
if(!preg_match('/Style# : ([\w-]+)/', $page->body, $m)) die('missing sku!');
$item['sku'] = $m[1];
Next we save the image, for later import/upload - identify the categories we care about - and construct our items. The options need to look like:
$options = array(
array('size' => '12', 'color' => 'purple'),
array('size' => '10', 'color' => 'yellow')
);
Where the array keys are the attributes that you have made configurable product attributes (Global, Dropdown, Is used in Configurable Products)
That's all there is to it. I won't go into the save function because hopefully that one will just work for you.