I am new to using Goutte to scrape websites, so I need some help.
Here is some example HTML code of the page for scraping:
<table class="table-main">
<tbody>
<tr class="tournament"><th class="h-text-left" colspan="2">France: Ligue 1</th></tr>
<tr data-dt="3,1,2022,14,00" data-def="0">
<td class="table-main__tt">Game 1</td>
</tr>
<tr data-dt="3,1,2022,18,00" data-def="0">
<td class="table-main__tt">Game 1</td>
</tr>
</tbody>
</table>
I want to get all games, but I want to categorize them to the corresponding country.
Here is my code, but it is not working:
$data = $crawler->filter('tbody')->each(function ($tr, $i) use($_date) {
$tr->filter('tr')->each(function ($td, $i) use($tr) {
if($td->attr('class') == 'tournament') {
$name = explode(':', $td->text());
$country = trim($name[0]);
}
else {
$game = $td->text();
return [
'country' => $country,
'game' => $game,
];
}
});
});
The problem is that is not "saving" the country for the lines after the first one. How can I achieve that?