-2

嗨,我正在使用 simple_html_dom 库来解析这个 html

<tr class="even">
<th>Yesterday</th>
<td class="avg">42.57%</td>
<td class="percent"> -0.06% </td>
<td class='arrow'> </td>
</tr>
<tr >
<th>7 day</th>
<td class="avg">41.79%</td>
<td class="percent"> +0.14% </td>
<td class='arrow'> </td>
</tr>
<tr class="even">
<th>1 month</th>
<td class="avg">42.10%</td>
<td class="percent"> -5.63% </td>
<td class='arrow'> </td>
</tr>
<tr >
<th>3 month</th>
<td class="avg">44.198%</td>
<td class="percent"> -2.28% </td>
<td class='arrow'> </td>
</tr>

我需要单独变量中的所有数据。如平均等...任何帮助将不胜感激。谢谢

4

1 回答 1

1

您应该能够像这样爬过它们:

<?php
    $simpleHTMLDom = new simple_html_dom();
    $simpleHTMLDom->load($html);
    $tableRows = $simpleHTMLDom->find('tr');

    $rows = array();
    if (!empty($tableRows)) {
        foreach ($tableRows as $tableRow) {
            $row = array();
            $tableColumns = $tableRow->find('td.avg, td.percent');
            if (!empty($tableColumns)) {
                foreach ($tableColumns as $tableColumn) {
                    $row[$tableColumn->class] = $tableColumn->innertext;
                }
            }
            $rows[] = $row;
        }
    }
    print_r($rows);
?>
于 2013-05-06T10:45:35.467 回答