1

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):

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?

Latheesan
  • 23,247
  • 32
  • 107
  • 201
  • What happens when you change `$node->filter('th .field')` to `$node->evaluate('th.field')`? BTW, `th .field` looks for _descendants_ of `th` with class `field`; whitespace has meaning in CSS selectors. – raina77ow Dec 31 '17 at 17:41
  • @raina77ow This `echo $node->evaluate('th.field')->first()->text();` also gives me same error. – Latheesan Dec 31 '17 at 18:27

0 Answers0