我无法到达表格的td标签

时间:2018-10-08 05:01:13

标签: php laravel domparser

我无法获取表格的td标签。我正在尝试第一个td属性,第二个是值。这是网站:

https://www.mecsumai.com/bkn-summary/?bid=16076001&bg1=026937&bg2=dce8e2&wdt=100%25&aln=center&newid=06076001

我的foreach代码是兔子:

$allTables = $DOMParser->getElementsByTagName('table');
        foreach ($allTables as $table) {
            foreach ($table->getElementsByTagName('tr') as $tr) {
                $tds = $tr->getElementsByTagName('td');
                if ($tds->length == 2) {
                    $property = trim($tds->item(0)->nodeValue);
                    $value = trim($tds->item(1)->nodeValue);

                    switch ($property) {
                        case '販売価額':
                            $changeForMyDB['price'] = $value;
                            break;
                        case '所在地':
                            $changeForMyDB['address'] = $value;
                            break;
                        case '総戸数':
                            $changeForMyDB['hows_old'] = $value;
                            break;
                        case '間取り':
                            $changeForMyDB['rooms'] = $value;
                            break;
                        case '建物竣工':
                            $changeForMyDB['old'] = $value;
                            break;
                        case '入居(予定)日':
                            $changeForMyDB['entery'] = $value;
                            break;
                        case 'バルコニー面積':
                            $changeForMyDB['balcon_m2'] = $value;
                            break;
                        case '管理会社':
                            $changeForMyDB['company_name'] = $value;
                            break;
                        case '物件名':
                            $changeForMyDB['name'] = $value;
                            break;
                        case '専有面積':
                            $changeForMyDB['extend'] = $value;
                            break;

                        default:
                            # code...
                            break;
                    }
                }
            }
        }

        Estates::insertGetId($changeForMyDB);
        $this->line('DATA saved.');
    }
}
}

我更新了代码,没有错误,但仍然无法获取数据。 我这是怎么了?
任何想法?谢谢!

1 个答案:

答案 0 :(得分:1)

您不能直接作为数组访问DOMNodeList,但是有一种等效的访问Nodes的方法。我还添加了一些惰性检查,以确保tr至少具有两个与属性和值相对应的元素:

$dom = DOMDocument::loadHTMLFile("https://www.mecsumai.com/bkn-summary/?bid=16076001&bg1=026937&bg2=dce8e2&wdt=100%25&aln=center&newid=06076001");

$allTables = $dom->getElementsByTagName('table');
foreach ($allTables as $table){
    foreach ($table->getElementsByTagName('tr') as $tr){
        $tds = $tr->getElementsByTagName('td');
        if ($tds->length == 2) {
            $property = trim($tds->item(0)->nodeValue);
            $value = trim($tds->item(1)->nodeValue);
            $d[$property] = $value;
        }
    }
    if (isset($d)) {
        echo "Keys: " .  count($d) . "\n";
        unset ($d);
    }
}

输出:

Keys: 31
Keys: 27
Keys: 30