解析表并删除多余的数据

时间:2015-10-18 13:49:14

标签: php simple-html-dom

我使用simpel dom parser来检索som数据。但是,当我更改表格布局时,解析器停止工作。我收到错误PHP Fatal error: Call to a member function find() on null

require('simple_html_dom.php');
$html = file_get_html($url);
$table = $html->find('table');
$rowData = array();

foreach($table->find('tr') as $row) {
    // initialize array to store the cell data from each row
    $stocks = array();
    foreach($row->find('td') as $cell) {
        // push the cell's text to the array
        $stocks[] = $cell->plaintext;
    }
    $rowData[] = $stocks;
}

echo '<table>';
foreach ($rowData as $row => $tr) {
    echo '<tr>'; 
    foreach ($tr as $td)
        echo '<td>' . $td .'</td>';
    echo '</tr>';
}
echo '</table>';

看看这个pastebin(对不起大表布局)。我想从这个表中提取以下内容

+--------+--------+------+----+------+-------+--------+--------+---------+
| Aktie  | Senast |  +/- |  % |  Köp |  Sälj |  Högst |  Lägst |  Omsatt |
+--------+--------+------+----+------+-------+--------+--------+---------+

此snipp有效,但它不会将数据排列在表格中:

require('simple_html_dom.php');
$html = file_get_html($url);

// remove all image
foreach($html->find('img') as $e)
    $e->outertext = '';

// Remove a attribute, set it's value as null! 
foreach($html->find('a') as $e)
$e->href = null;

// Find all <td> in <table> which class=hello 
foreach($html->find('table tr') as $es)
echo $es->innertext . '<br>';

我的问题:

  • 我如何获取上述th?并插入td列的相应数据?

预期结果是:

+-------------+--------+-------+-------+-------+-------+--------+--------+---------+
|   Aktie     | Senast |  +/-  |   %   |  Köp  |  Sälj |  Högst |  Lägst |  Omsatt |
+-------------+--------+-------+-------+-------+-------+--------+--------+---------+
| AAK AB      | 549,90 | ..etc | ..etc | ..etc | ..etc | ..etc  | ..etc  | ..etc   |
| ABB LTD     | 149.80 | ..etc | ..etc | ..etc | ..etc | ..etc  | ..etc  | ..etc   |
| and so on.. |        |       |       |       |       |        |        |         |
+-------------+--------+-------+-------+-------+-------+--------+--------+---------+

1 个答案:

答案 0 :(得分:0)

你有多张桌子吗?如果是,explicitly identify the table id

获取标题:$e = $table->find('thead');do something to it获取表数据,就像您已经做过的那样,我认为索引可以隐式地创建标题和数据之间的关系。