刮取表中的所有数据的问题

时间:2015-02-07 02:37:07

标签: php simple-html-dom

我在从两个特定的html表中获取所需的所有数据时遇到了一些问题。本帖子底部的表格。

上面的代码说明了html表id“table1”。我还需要以完全相同的格式从名为“table2”的表中获取值。我已经尝试过这段代码并且可以提取td值,但不能提取td中范围说明符中的少数值。我已经尝试了多种方法来做到这一点,但我只是没有得到它。我的代码看起来像:

$dom = file_get_html("internets.html);
         
         //not sure how to specify the table exactly!? because this code didn't work.
         //$tds = $dom->find('table[id=table1]',0)->find('tr');
         
         foreach($dom->find('tr') as $key => $tr)
         {   
           
            $td = $tr->find('td');
            echo $td[0]->innertext . "</br>";
          
         }

非常感谢任何帮助。我在这里做了一些搜索,也使用了简单的php dom手册。

以下是表的格式:

    <table id="table1">
        <tbody>
            <tr>
                <th width="48%" scope="row">

                    Prev Close:

                </th>
                <td class="yfnc_tabledata1">

                    0.02

                </td>
            </tr>
            <tr>
                <th width="48%" scope="row">

                    Open:

                </th>
                <td class="yfnc_tabledata1">

                    0.02

                </td>
            </tr>
            <tr>
                <th width="48%" scope="row">

                    Bid:

                </th>
                <td class="yfnc_tabledata1">
                    <span id="yfs_b00_pgo.ax">

                        0.0180

                    </span>
                </td>
            </tr>
            <tr>
                <th width="48%" scope="row"></th>
                <td class="yfnc_tabledata1"></td>
            </tr>
            <tr>
                <th width="48%" scope="row">

                    1y Target Est:

                </th>
                <td class="yfnc_tabledata1">

                    N/A

                </td>
            </tr>
            <tr>
                <th width="48%" scope="row">

                    Beta:

                </th>
                <td class="yfnc_tabledata1">

                    N/A

                </td>
            </tr>
            <tr>
                <th width="54%" scope="row">

                    Next Earnings Date:

                </th>
                <td class="yfnc_tabledata1">

                    N/A

                </td>
            </tr>
        </tbody>
    </table>

1 个答案:

答案 0 :(得分:0)

<?php

$html=<<<XHTML
<table id="table1">
        <tbody>
            <tr>
                <th width="48%" scope="row">

                    Prev Close:

                </th>
                <td class="yfnc_tabledata1">

                    0.02

                </td>
            </tr>
            <tr>
                <th width="48%" scope="row">

                    Open:

                </th>
                <td class="yfnc_tabledata1">

                    0.02

                </td>
            </tr>
            <tr>
                <th width="48%" scope="row">

                    Bid:

                </th>
                <td class="yfnc_tabledata1">
                    <span id="yfs_b00_pgo.ax">

                        0.0180

                    </span>
                </td>
            </tr>
            <tr>
                <th width="48%" scope="row"></th>
                <td class="yfnc_tabledata1"></td>
            </tr>
            <tr>
                <th width="48%" scope="row">

                    1y Target Est:

                </th>
                <td class="yfnc_tabledata1">

                    N/A

                </td>
            </tr>
            <tr>
                <th width="48%" scope="row">

                    Beta:

                </th>
                <td class="yfnc_tabledata1">

                    N/A

                </td>
            </tr>
            <tr>
                <th width="54%" scope="row">

                    Next Earnings Date:

                </th>
                <td class="yfnc_tabledata1">

                    N/A

                </td>
            </tr>
        </tbody>
    </table>
XHTML;

$dom = new DOMDocument;
$dom->loadHTML($html);

$xp = new DOMXPath($dom);
foreach ($xp->query("/*//table[@id='table1'//*/td") as $i=>$node) {
    echo $node->nodeValue;
}

?>
相关问题