i am using this code to scrap the amazon.com
$ch = curl_init(); // create a new cURL resource
$url='http://www.amazon.com/s/ref=sr_pg_1?rh=n%3A133140011%2Ck%3Aenglish+literature&sort=paidsalesrank&keywords=english+literature&ie=UTF8&qid=1327432144';
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch); // grab URL and pass it to the browser
//echo $data; ok till now
curl_close($ch);
$dom = new DOMDocument();
@$dom->loadHTML($data); // avoid warnings
$xpath = new DOMXPath($dom);
//getting titles
$book_t = $xpath->query('//div[@class="title"]/a[@class="title"]');
foreach ($book_t as $tag) {
print_r(trim($tag->nodeValue));
echo '<br/>';
}
$author = $xpath->query('//div[@class="title"]/span[@class="ptBrand"]');
foreach ($author as $tag) {
echo '<br/>';
//print_r($tag->nodeValue);
$s=$tag->nodeValue;
print_r(str_replace('by ', '', $s));
echo '<br/>';
}
Up to this step, i am okay, now i want to save this in csv file, but i don't know how to do it can somebody please help me? how should i code it? if you provide me code, my learning will be better.
also, does this code need improvement? if yes, how?