$arr =array(
("link to scrape"),
("link to scrape")
);
我正在使用两个链接进行刮擦。
foreach($arr as $value){
$html = file_get_contents($value);
//get the html returned from the following url
$pokemon_doc = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
if(!empty($html)){ //if any html is actually returned
$pokemon_doc->loadHTML($html);
libxml_clear_errors();
$pokemon_xpath = new DOMXPath($pokemon_doc);
$pokemon_row = $pokemon_xpath->query("//*[contains(@class, 'product-title')]/*[2]");
if($pokemon_row->length > 0){
foreach($pokemon_row as $row){
$list = $row->nodeValue ;
echo $list;
$lists = explode(',',$list);//convert string to array
}
echo "<a href='demosaved.csv' download>download</a>";
$file = fopen('demosaved.csv', 'a');// create csv file
foreach($lists as $line){ // store data in csv
fputcsv($file, $line);
}
fclose($file);
}
}
}
我正在使用两个链接,所以我应该为每个链接获得两个结果,但我只得到第二个链接的结果。