Home > Mobile >  LARAVEL: Goutte returns empty array
LARAVEL: Goutte returns empty array

Time:05-29

I'm using Goutte (a web scraper) for the first time. I'm following a tutorial that my teacher made and I'm following it with much attention. However, when I'm trying to dump and die the scraped data, it always returns an empty array and I can't see where my fault is. Can anyone see what I'm doing wrong?

My code:

      private function scrapeMazoet($url)
      {
         $client = new Client();
         $crawler = $client->request('GET', $url);

        $categories = $crawler->filter('.main-menu-container .menu #menu-item-454 .sub-menu #menu-item-2483 .ul-multiple-col #menu-item-3827 a')
            ->each(function($node) {
                  $title = $node->text();
                  dump($title);
            });
            dd($categories);
      }

CodePudding user response:

Keep things simple first. Iterate through the set returned and push each value to an empty array then display the array and look at your results. After that try and use the other functions.

 $client = new Client();
 $crawler = $client->request('GET', $url);
 $array = [];
 for ($i = 0; $i < count($crawler); $i  ) {
    //process information here
    array_push($array, $crawler[i]);
 }
 print($array)
 

CodePudding user response:

I figured it out by working step by step instead of trying to give the full path in one take. Some inputs where not recognisable by Goutte so had to grab the info in a different way. I'll show the bad and good method as an example:

The wrong way:

$categories = $crawler->filter('.main-menu-container .menu #menu-item-454 .sub-menu #menu-item-2483 .ul-multiple-col #menu-item-3827 a')

The correct way:

$categories = $crawler->filter('#menu-item-2483 ul li a')

I think you should make it yourself easy and start with the most closest id to start and work your way through.

  • Related