I am scraping with the package fabpot/goutte
and with the method each
the variable $config
does not have a scope. I don't know how to do it so as not to have to declare it again within the method.
$config = Config::get('scraper.small_appliances');
$crawler = $client->request('GET', $config['url']);
$products = $crawler->filter($config['item'])->each(function($node) {
// única forma que he encontrado para que funcione
// $config = Config::get('scraper.small_appliances');
$price = $node->filter($config['price'])->first()->html();
$name = $node->filter($config['name'])->first()->html();
return [
"price" => $price,
"name" => $name
];
});
In case it's worth something I put the method here
/**
* Calls an anonymous function on each node of the list.
*
* The anonymous function receives the position and the node wrapped
* in a Crawler instance as arguments.
*
* Example:
*
* $crawler->filter('h1')->each(function ($node, $i) {
* return $node->text();
* });
*
* @param \Closure $closure An anonymous function
*
* @return array An array of values returned by the anonymous function
*/
public function each(\Closure $closure)
{
$data = [];
foreach ($this->nodes as $i => $node) {
$data[] = $closure($this->createSubCrawler($node), $i);
}
return $data;
}
each
has an anonymous function inside with its own scope.use
Being that way
References