我对此很陌生,所以我可能会走完全错误的方向。
我正在使用网络爬虫库,我想为我抓取的每个页面返回一组结果。不过,我的问题不在于刮刀。
我从一个索引数组 $links 开始,其值是我想要抓取信息的 url。 如果我去:
foreach ($links as $key=>$value) {
$crawler = $client->request('GET', $value);
$crawler->filter('p.scores')->each(function ($node) {
$thisscore = $node->text();
print $thisscore;
});
};
它打印出屏幕上的所有结果,但我希望每个 foreach 循环的结果都在不同的数组中,即最终以 @results0 @results1 @results2
最佳答案
只需创建一个数组并在每次迭代时填充它
$new_array = array();
foreach ($links as $key=>$value) {
$crawler = $client->request('GET', $value);
$crawler->filter('p.scores')->each(function ($node) {
$thisscore = $node->text();
// print $thisscore;
$new_array[] = $thisscore;
});
};
现在您的 $new_array
中已包含所有结果
关于php - 在 foreach 的每次迭代中创建新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21810937/