2015-02-07 42 views
0

我遇到了一些問題,需要從兩個特定的html表中獲取所需的所有數據。此帖子底部的表格。刮掉表格中所有數據的問題

上面的代碼聲明瞭html table 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>

回答

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; 
} 

?>