Skip to content
Advertisement

Goutte laravel web scrap from all table data

I need to scrap all Th and td data return like an array. Result showing only first Th/td data.

$client = new Client();
$url ='https://schools.world-schools.com/school/munich-international-school/';

$page = $client->request('GET', $url);

$page->filter('table')->each(function ($item) {
    $this->results[$item->filter(' tr > th')->text()]= $item->filter('tr > td')->text();
});

dd( $this->results);

result showing only first Th/td, need to all row:

array:1 [▼
  "Year of Foundation" => "1966"
]

Advertisement

Answer

The provided code is looping every table ($page->filter('table')->each) and selecting only a single tr > th. But what you want is to loop the tr‘s of the table.

Probably you will need to do something like the following:

$page->filter('#new_school_info tr')
    ->each(function ($item) {
        $header = $item->filter('th')->first();

        if ($header === null) {
            return;
        }

        $data = $item->filter('td')->first();

        $this->result[$header->text()] = $item->filter('td')->first()?->text;
        
        // Or for php versions below 8
        // $item->filter('td')->first()
        // 
        // $this->result[$header->text()] = $data ? $data->text() : null;
    });

Have not tested this myself.

Tip: Take a look at the source of the page and try to make the selectors.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement