2014-11-01 1 views
0

내 코드에 어떤 문제가 있습니까? 왜 효과가 없습니까? PHP simple_html_dom URL에서 데이터 추출

내가 사용하려고 내 코드입니다 :

function extract_data($url){ 

    // Create DOM from URL 
    $html = file_get_html($url); 

    // initialize empty array to store the data array from each row 
    $theData = array(); 

    // loop over rows 
    foreach($html->find('p.ch_title') as $row) { 

     // initialize array to store the cell data from each row 
     $rowData = array(); 
     foreach($row->find('p.ch_spec') as $cell) { 

      // push the cell's text to the array 
      $rowData[] = $cell->innertext; 
     } 

     // push the row's data array to the 'big' array 
     $theData[] = $rowData; 
    } 

    return $theData; 

} 

을하고이 URL에서 HTML 데이터입니다;

<div class="holder-specificatii"> 
     <div class="box-specificatie"> 
      <div class="ch_group">Dimensiuni</div> 
      <p class="ch_title">Latime (mm):</p> 
      <p class="ch_spec">195</p> 
      <p class="ch_title">Inaltime:</p> 
      <p class="ch_spec">65</p> 
      <p class="ch_title">Diametru (inch):</p> 
      <p class="ch_spec">15</p> 
      <div class="clear"></div> 
     </div> 
     <div class="box-specificatie"> 
      <div class="ch_group">Caracteristici tehnice</div> 
      <p class="ch_title">Anotimp:</p> 
      <p class="ch_spec">Iarna</p> 
      <p class="ch_title">Indice sarcina:</p> 
      <p class="ch_spec">91</p> 
      <p class="ch_title">Indice viteza:</p> 
      <p class="ch_spec">T</p> 
      <p class="ch_title">Economie de carburant:</p> 
      <p class="ch_spec">C</p> 
      <p class="ch_title">Franare pe suprafete umede:</p> 
      <p class="ch_spec">C</p> 
      <p class="ch_title">Tip vehicul:</p> 
      <p class="ch_spec">Turism</p> 
      <p class="ch_title">DOT:</p> 
      <p class="ch_spec">2014</p> 
      <p class="ch_title">Nivel de zgomot (dB):</p> 
      <p class="ch_spec">72dB</p> 
      <div class="clear"></div> 
     </div> 
    </div> 

문제는 빈 배열을 반환하는 기능입니다. 당신은 정의되지 않은 객체를 가리키고

+0

정말 좁혀 야하거나 더 구체적으로해야합니다. – pguardiario

+1

for 루프에서 정의되지 않은'$ table' 객체를 사용하고 있습니까? 그게 당신에게 오류 메시지를 제공해야합니다! –

+0

예, 사소한 문제였습니다. $ table to $ html로 다시 작성했지만 빈 배열을 반환합니다. – m3tsys

답변

1

, 대신 $html를 사용한다 : 첫 번째 foreach 문에서

function extract_data($url){ 

    $html = file_get_html($url); 
    $theData = array(); 
    // loop over rows 
    foreach($html->find('div.box-specificatie') as $k => $row) { // loop each container 
     $temp = array(); 
     // $main_title = $row->find('div.ch_group', 0)->innertext; 
     foreach($row->find('p.ch_title') as $title) { // each title 
      $spec = $title->next_sibling()->innertext(); // pair up with spec 
      $temp[] = array('title' => $title->innertext, 'spec' => $spec); 
     } 
     $theData[$k] = $temp; // push inside 
     // $theData[$main_title] = $temp; // optionally you can use a main title 

    } 

    return $theData; 
} 

echo '<pre>'; 
print_r(extract_data($url)); 
+0

각 제목에 맞춤 문자열을 추가하는 방법이 있습니까? – m3tsys

+1

@ m3tsys 맞춤 문자열이란 무엇입니까? 당신이 이미'$ title-> innertext'라는 제목을 가지고 있기 때문에, 당신이해야 할 일은 무엇이든 할 수 있습니다. – Ghost

+0

@ m3tsys 당신은이 요소를 의미합니까? '

Dimensiuni
'? – Ghost

0

당신이 바로 일을, 당신은 file_get_html에서하지만 당신이 중첩 된 foreach 문에서받은 HTML을 사용 반환 된 $ 행을 사용하고 p.ch_spec 원인이 p.ch_title의 자식이 아닙니다.