我需要在csv中获取抓取的输出,但我在csv中只得到一个结果

时间:2017-07-22 11:46:27

标签: php xpath web-scraping screen-scraping fputcsv

$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);
        }
    }
}

我正在使用两个链接,所以我应该为每个链接获得两个结果,但我只得到第二个链接的结果。

0 个答案:

没有答案