I have a page that renders html like this:
<div class="details">
<table>
<tr>
<th class="field">Title</th>
<td class="value">Laravel for Noobs</td>
</tr>
<tr>
<th class="field">Type</th>
<td class="value">PDF Book</td>
</tr>
</table>
</div>
What I want to do is iterate through each <tr></tr>
and parse the value of the <th class="field">???</th>
and <td class="value">???</td>
.
For example, I want to generate an array like this from the above html:
$details = [
['field' => 'Title', 'value' => 'Laravel for Noobs'],
['field' => 'Type', 'value' => 'PDF Book']
];
Based on what I can see from the documentation(s):
- https://github.com/dweidner/laravel-goutte
- https://github.com/FriendsOfPHP/Goutte
- https://symfony.com/doc/current/components/dom_crawler.html
I tried the following:
$details = [];
$crawler->filter('.details table tr')->each(function ($node) use(&$details) {
$field = $node->filter('th .field')->first()->text();
$value = $node->filter('td .value')->first()->text();
$details[] = [
'field' => $field,
'value' => $value
];
});
When I try the above, I get the following error:
The current node list is empty.
Any ideas on how to achieve this? Is it possible to further filter a node, after initial filter?