2012-10-01 83 views
0

我要解析2種表 之一是這個簡單的HTML DOM和分析表

http://leghe.fantagazzetta.com/f12-13/( 「Classifica興業」)

和其他兩個表在這裏 http://leghe.fantagazzetta.com/f12-13/formazioni?id=30339&g=4

如何我可以將這些數據提取到數組中嗎?

第一個表我寫了這個代碼,但我真的不知道我在做什麼

<?php 
require('simple_html_dom.php'); 

$html = new simple_html_dom(); 
$html->load_file('http://leghe.fantagazzetta.com/f12-13/classifica'); 

$tabClassifica = $html->find('table#classifica tr'); 

foreach($tabClassifica as $n) { 
    $team=$n->find('td',0)->outertext; 
    $arrayTeams[] = array('teamname' => $team); 
} 

?> 
<pre> 
<? print_r($arrayTeams); ?> 
</pre> 

我已經得到這個陣列結構

[1] => Array 
    (
     [TeamName] => A.C. Tua 
     [Pt.] => 9 
     [G] => 4 
     [V] => 3 
     [N] => 0 
     [P] => 1 
     [G+] => 8 
     [G-] => 5 
     [Somma Punti] => 293,50   
    ) 

[2] => Array 
    (
     [TeamName] => Ehi Team 
     [Pt.] => 7 
     [G] => 4 
     [V] => 2 
     [N] => 1 
     [P] => 1 
     [G+] => 5 
     [G-] => 5 
     [Somma Punti] => 279,50  
    ) 

[3] => Array 
    (
     [TeamName] => Brontolo      

有人能指導我?

回答

3

您可以使用PHPQuery來實現你想要的。

下面是一個例子代碼:

include('phpQuery-onefile.php'); 

$content = file_get_contents('http://leghe.fantagazzetta.com/f12-13/'); 
$html = phpQuery::newDocumentHTML($content); 

$table = $html->find('#classifica tbody'); 
$general_ranking = array(); 
$i=0; 
foreach($table->children('tr') as $tr){ 
    /** 
    * @var DOMElement $tr 
    */ 
    $getTd = $tr->getElementsByTagName('td'); 
    foreach($getTd as $td){ 
     /** 
     * @var DOMElement $td 
     */ 
     $general_ranking['tr_'.$i][] = trim($td->textContent); 
    } 
    ++$i; 
} 

這應該顯示類似於:

array (size=6) 
    'tr_0' => 
    array (size=7) 
     0 => string 'A.C. Tua' (length=8) 
     1 => string '9' (length=1) 
     2 => string '4' (length=1) 
     3 => string '3' (length=1) 
     4 => string '0' (length=1) 
     5 => string '1' (length=1) 
     6 => string '293,50' (length=6) 
    'tr_1' => 
    array (size=7) 
     0 => string 'Ehi Team' (length=8) 
     1 => string '7' (length=1) 
     2 => string '4' (length=1) 
     3 => string '2' (length=1) 
     4 => string '1' (length=1) 
     5 => string '1' (length=1) 
     6 => string '279,50' (length=6) 
    'tr_2' => 
    array (size=7) 
     0 => string 'Brontolo' (length=8) 
     1 => string '7' (length=1) 
     2 => string '4' (length=1) 
     3 => string '2' (length=1) 
     4 => string '1' (length=1) 
     5 => string '1' (length=1) 
     6 => string '274,50' (length=6) 
    'tr_3' => 
    array (size=7) 
     0 => string 'milanelcuore' (length=12) 
     1 => string '6' (length=1) 
     2 => string '4' (length=1) 
     3 => string '2' (length=1) 
     4 => string '0' (length=1) 
     5 => string '2' (length=1) 
     6 => string '281,00' (length=6) 
    'tr_4' => 
    array (size=7) 
     0 => string 'LONGOBARDA' (length=10) 
     1 => string '5' (length=1) 
     2 => string '4' (length=1) 
     3 => string '1' (length=1) 
     4 => string '2' (length=1) 
     5 => string '1' (length=1) 
     6 => string '254,50' (length=6) 
    'tr_5' => 
    array (size=7) 
     0 => string 'i puffi' (length=7) 
     1 => string '0' (length=1) 
     2 => string '4' (length=1) 
     3 => string '0' (length=1) 
     4 => string '0' (length=1) 
     5 => string '4' (length=1) 
     6 => string '258,00' (length=6) 

編輯:
回答我有興趣simple_html_dom後,所以我決定嘗試出來。 編碼風格比PHPQuery容易一些,但我認爲它並不那麼穩定。

無論如何,這裏是你需要得到它的工作代碼:

include('simple_html_dom/simple_html_dom.php'); 

$html = new simple_html_dom(); 
$html->load_file('http://leghe.fantagazzetta.com/f12-13/classifica'); 

$tabClassifica = $html->find('table#classifica tr'); 

foreach($tabClassifica as $n) { 
    /** 
    * @var simple_html_dom_node $n; 
    */ 
    $tds = $n->find('td'); 

    // Don't allow empty records. 
    $team = trim(strip_tags($tds[0]->innertext)); 
    if($team == "" || $team == "&nbsp;") continue; 

    $arrayTeams[] = array(
     'TeamName' => $team, 
     'Pt.' => trim(strip_tags($tds[1]->innertext)), 
     'G' => trim(strip_tags($tds[2]->innertext)), 
     'V' => trim(strip_tags($tds[3]->innertext)), 
     'N' => trim(strip_tags($tds[4]->innertext)), 
     'P' => trim(strip_tags($tds[5]->innertext)), 
     'G+' => trim(strip_tags($tds[6]->innertext)), 
     'G-' => trim(strip_tags($tds[7]->innertext)), 
     'Somma Punti' => trim(strip_tags($tds[8]->innertext)), 
    ); 
}