0

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?

khr055
  • 28,690
  • 16
  • 36
  • 48
Zaffar Saffee
  • 6,167
  • 5
  • 39
  • 77
  • please point out why none of http://stackoverflow.com/search?q=write+csv+file+php did solve your question – Gordon Jan 24 '12 at 23:17
  • i mean CSV , @Gordon , i tried the search using writing csv through dom document, but could not get what i want, also i am confused because i dont know how two different node values will be combined. what i mean is, i want a list in which first value be title and 2nd value will be author name, and then repeating again – Zaffar Saffee Jan 24 '12 at 23:24
  • 2
    Break the problem down. DOMDocument gets you data *out of* the page. Take that extracted data and put it in an array. When you're done extracting the data you need, output it to a CSV file. DOMDocument and CSV are not connected, they're two separate steps. – deceze Jan 24 '12 at 23:41
  • @deceze thanks for the help, if this is not possible, how to convert these arrays to xml? then i will try to put it as CSV. i am so much confused over the problem that i get 2 array for 2 nodes, while i want to combine these tow arrays togetter..i am so much confused. – Zaffar Saffee Jan 24 '12 at 23:58

1 Answers1

0

there's a function fputcsv ... you can use it like this:

<?php 
$list = array (
   array('aaa', 'bbb', 'ccc', 'dddd'),
   array('123', '456', '789'),
   array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');//your file csv

foreach ($list as $fields) {
  fputcsv($fp, $fields);
}

fclose($fp);
?>

each array in the array list will be a line in the csv file..

Amazone
  • 426
  • 4
  • 14