我无法到达 table 的 td 标签
I can't reach the table's td tag
我无法获取 table 的 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.');
}
}
}
我更新了代码,没有出现错误,但仍然无法获取数据。
我在这里做错了什么?
任何的想法?谢谢!
您不能将 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
我无法获取 table 的 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.');
}
}
}
我更新了代码,没有出现错误,但仍然无法获取数据。
我在这里做错了什么?
任何的想法?谢谢!
您不能将 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