Goutte - Check if there are two nodes

Viewed 181

I am using php 7.4.1 and "fabpot/goutte": "^3.3".

I am having the following script:

<?php
require_once '../vendor/autoload.php';

use Symfony\Component\DomCrawler\Crawler;
use Goutte\Client;

try {

        $resArr = array();
        $tempArr = array();

        $url = "https://edikte.justiz.gv.at/edikte/ex/exedi3.nsf/0/19dd135274ceb842c12586390028507e?OpenDocument&f=1&bm=2";

        // get page
        $client = new Client();
        $content = $client->request('GET', $url)->html();
        $crawler = new Crawler($content, null, null);
        $table = $crawler->filter('#diveddoc > div:nth-child(2) > table')->first()->closest('table');

        $table->filter('tr')
            ->each(function (Crawler $tr) use (&$firm, &$resArr, &$tempArr) {

                $val = addScrappedTextToArr($tr, 'PLZ/Ort:');
                list($tempArr, $val) = checkNullAddArr($val, "plz_ort", $tempArr);

                $val = addScrappedTextToArr($tr, 'Objektgröße:');
                list($tempArr, $val) = checkNullAddArr($val, "objektGroesse", $tempArr);

            });

        array_push($resArr, $tempArr);

        var_dump($resArr);
} catch (Exception $e) {
    report($e);
}


function checkNullAddArr($val, $key, $tempArr)
{
    if (!is_null($val)) {
        $tempArr[$key] = $val;
        $val = null;
    }
    return array($tempArr, $val);
}

function addScrappedLinkToArr(Crawler $tr, $scrapVal)
{
    if (strpos($tr->text(), $scrapVal) !== false) {
        $val = "https://edikte.justiz.gv.at" . trim($tr->filter('td > a')->attr("href"));
        return $val;
    }
}

function addScrappedTextToArr(Crawler $tr, $scrapVal)
{
    /*
    if ($tr->filter('td')->count() >= 2) {
        $label = $tr->filter('td.tlabel')->text();
*/
    if (strpos($tr->text(), $scrapVal) !== false) {
        $val = trim(str_replace([$scrapVal], "", $tr->text()));
        return $val;
        // array_push($resArr, $val);
    }
    // }
    // return $arr;
}

As you can see the output is the following for the array-key objectGroesse:

enter image description here

However, I would like to get the yellow text instead of the red underlined text:

enter image description here

As I am simply trying to match all the strings the word Objektgröße: is found in the larger text and matched.

I tried the following by simply rewriting the function addScrappedTextToArr() to filter on the label:

function addScrappedTextToArr(Crawler $tr, $scrapVal)
{

    if ($tr->filter('td')->count() >= 2) {
        $label = $tr->filter('td.tlabel')->text();

        if (strpos($tr->text(), $scrapVal) !== false) {
            $val = trim(str_replace([$scrapVal], "", $tr->text()));
            return $val;
            // array_push($resArr, $val);
        }
    }
    // return $arr;
}

However I get the following error:

The current node list is empty.

Any suggestions how to fix my above bug?

I appreciate your replies!

1 Answers

You get the error because even while your condition for extracting the label works for rows like this one:

<tr>
    <td class="tlabel">Grundbuch:</td>
    <td class="ttext">04018&nbsp;Leobersdorf</td>
</tr>

the table also contains this row:

<tr>
    <td class="flabel">EZ:</td>
    <td class="ftext">2074</td>
</tr>

In your condition you only check whether the row contains at least two <td> cells and then assume that the row contains a cell with class tlabel:

if ($tr->filter('td')->count() >= 2) {
    $label = $tr->filter('td.tlabel')->text();

This fails for the row mentioned above as it contains a <td> with class flabel instead of tlabel.

Related