使用domphp从html表中获取数据
我有一个错误:使用domphp从html表中获取数据,php,Php,我有一个错误: Error Trying to get property of non-object 'Attrib2' => $cols->item(1)->nodeValue 代码是: $dom->preserveWhiteSpace = false; $tables = $dom->getElementsByTagName('table'); $rows = $tables->item(0)->getElementsB
Error Trying to get property of non-object
'Attrib2' => $cols->item(1)->nodeValue
代码是:
$dom->preserveWhiteSpace = false;
$tables = $dom->getElementsByTagName('table');
$rows = $tables->item(0)->getElementsByTagName('tr');
$i=0;
foreach ($rows as $row)
{
/*** get each column by tag name ***/
$cols = $row->getElementsByTagName('td');
$this->data['Table'][$i] = array(
'Attrb1' => $cols->item(0)->nodeValue,
'Attrib2' => $cols->item(1)->nodeValue
);
$i++;
}
}
return $this->toArray();
}
'Attrib2' => $cols->item(1)->nodeValue
我遇到此错误的行是:
'Attrib2' => $cols->item(1)->nodeValue
html代码是:
'Attrib2' => $cols->item(1)->nodeValue
<table border=1 align="center" cellpadding=5 width="95%">
<!doctype html public "-//w3c//dtd html 4.0//EN">
<html it>
<head>
<meta name="Generator" content="OLS">
</head>
<body>
<td colspan=2 align="center">
<b>
<i>
Attrib1
</i>
</b>
</td>
<td>
<b>
<i>
Attrib2
</i>
</b>
</td>
<td>
<b>
<i>
<tr>
<td>
A000211
</td>
<td nowrap>
Statistic
</td>
</tr>
属性1
属性2
A000211
统计资料
但我不知道问题是否出在foreach中,或者我所做的所有get数据都是一个大错误,或者我以错误的方式处理html..请帮助我..您可以使用:
'Attrib2' => $cols->item(1)->nodeValue
$dom->loadHTML($result);
libxml_clear_errors();
libxml_use_internal_errors($errors);
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
/*** the table by its tag name ***/
$tables = $dom->getElementsByTagName('table');
/*** get all rows from the table ***/
$rows = $tables->item(1)->getElementsByTagName('tr');
/*** loop over the table rows ***/
$i=0;
foreach ($rows as $row) {
/*** get each column by tag name ***/
$cols = $row->getElementsByTagName('td');
$this->data['List'][$i] = array(
'name1' => $cols->item(1)->nodeValue,
'name2' => $cols->item(2)->nodeValue,
'name3' => $cols->item(3)->nodeValue,
'name4' => $cols->item(4)->nodeValue,
'name5' => $cols->item(5)->nodeValue,
'name6' => $cols->item(6)->nodeValue,
'name7' => $cols->item(7)->nodeValue
);
$i++;
}
错误表明该行中没有两个TDs。-这是有意义的,因为HMTL的快速扫描显示了一些不规则,加载时可能会得到纠正。-请将您的问题隔离到您从头创建的示例中,该示例包含演示您的问题所需的尽可能少的数据和代码。此外,我还建议创建一个HTML表格模型来处理表格,这样您就可以更轻松地访问行和列。虽然TDs不是8,但如果我尝试将其全部放在表中,我仍然有此错误。而且我无法更改HTML。行的示例是:0065207 Telematic 18 6 04/09/2012 2011/2012 Check 2008/2009