【问题标题】:Goutte - Check if there are two nodesGoutte - 检查是否有两个节点
【发布时间】:2021-06-20 21:08:03
【问题描述】:

我正在使用php 7.4.1"fabpot/goutte": "^3.3"

我有以下脚本:

<?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;
}

如您所见,数组键 objectGroesse 的输出如下:

但是,我想得到黄色文本而不是红色下划线文本:

因为我只是想匹配所有字符串,所以在较大的文本中找到并匹配了单词 Objektgröße:

我通过简单地重写函数addScrappedTextToArr() 来过滤标签来尝试以下操作:

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;
}

但是我得到以下错误:

The current node list is empty.

有什么建议可以解决我上面的错误吗?

感谢您的回复!

【问题讨论】:

    标签: php goutte


    【解决方案1】:

    您收到错误是因为即使您提取标签的条件适用于这样的行:

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

    表格也包含这一行:

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

    在您的情况下,您只检查该行是否包含至少两个&lt;td&gt; 单元格,然后假设该行包含一个具有tlabel 类的单元格:

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

    上面提到的行失败了,因为它包含一个 &lt;td&gt;flabel 而不是 tlabel

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 2022-07-20
      • 2023-03-19
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多