2017-04-24 53 views
-1

我想從這個網站報廢數據https://www.flightradar24.com/data/flights/southwest-airlines-wn-swaPHP網頁從表中的HTML標籤刮

寫的亞當先生此頁上的代碼帶回的所有內容然而 我想在臺只返回9條三,從夏洛,華盛頓和分別哥倫布過濾的

Flight From   To  Aircraft Registration 
============================================================================  
WN8  Charlotte (CLT) Houston (HOU) B737  N7716A  Live  
WN9  Charlotte (CLT) Houston (HOU) B733  N7716A  Live  
WN10 Charlotte (CLT) Houston (HOU) B737  N7716A  Live  
WN21 Washington (DCA) Orlando (MCO) B743  N568WN  Live  
WN22 Washington (DCA) Orlando (MCO) B755  N568WN  Live  
WN23 Washington (DCA) Orlando (MCO) B776  N568WN  Live 
WN119 Columbus (CMH) Fort Myers  B712  N964WN  Live  
WN120 Columbus (CMH) Fort Myers  B732  N964WN  Live  
WN121 Columbus (CMH) Fort Myers  B764  N964WN  Live 

現在亞當先生的代碼看起來像下面這樣,它把所有的數據從該網站,但我需要的是在下面的代碼,以一些小改變只是如上所示給我一個小過濾樣本。

<?php 
$url = "https://www.flightradar24.com/data/flights/southwest-airlines-wn-swa"; 
$html = file_get_contents($url); 
libxml_use_internal_errors(true); 
$doc = new \DOMDocument(); 
if($doc->loadHTML($html)) 
{ 
    $result = new \DOMDocument(); 
    $result->formatOutput = true; 
    $table = $result->appendChild($result->createElement("table")); 
    $thead = $table->appendChild($result->createElement("thead")); 
    $tbody = $table->appendChild($result->createElement("tbody")); 

    $xpath = new \DOMXPath($doc); 

    $newRow = $thead->appendChild($result->createElement("tr")); 

    foreach($xpath->query("//table[@id='tablepress-2']/thead/tr/th[position()>0]") as $header) 
    { 
     $newRow->appendChild($result->createElement("th", trim($header->nodeValue))); 
    } 

    foreach($xpath->query("//table[@id='tablepress-2']/tbody/tr") as $row) 
    { 
     $newRow = $tbody->appendChild($result->createElement("tr")); 

     foreach($xpath->query("./td[position()>0 and position()<6]", $row) as $cell) 

     { 
      $newRow->appendChild($result->createElement("td", trim($cell->nodeValue))); 
     } 
    } 

    echo $result->saveXML($result->documentElement); 
} 
?> 

我創建了一個數組[休斯頓,夏洛特,華盛頓】美國我會用它來過濾和循環比較的,所以我內嵌它到主循環,但它 似乎並沒有工作,我的意思是我不知道我是否在這裏做正確的事情。

$states = array("huston,charlotte,washington"); 

foreach($xpath->query("./td[position()>0 and position()<6]", $row) as $cell) 

     {   
      for ($x = 0; $x <= 10; $x++) 
       { 

       if($xpath->query("./td[position()=2", $row)==$x) 
      { 
        $newRow->appendChild($result->createElement("td", trim($cell->nodeValue))); 
      } 
       } 


     }  

我將不勝感激爲其提供任何幫助,謝謝

+0

本網站不是爲您的項目獲得免費工作人員而設計的。請告訴我們你已經嘗試了什麼。 – Peter

+0

這很公平,好吧,我正在看循環中的這個區域 – Bels

+0

你在最後@Bels做了什麼? – Lissy

回答

0

更好的選擇

網站刮不理想,它的速度慢,亂了,只要這個網站的更新,你的代碼會中斷。一個更好的選擇是使用API​​來獲取這些數據,例如http://uk.flightaware.com/commercial/flightxml/它有清晰的文檔,你幾乎可以複製和粘貼他們的代碼片段來獲取你需要的數據。 (還有一些其他網站也有類似的API,所以請在Google上查看)。

在回答你的問題

如果你真的想使用PHP的Web刮HTML表格,然後這樣的事情應該工作(改編自Steve Lacey's之一回購):

<?php 

$doc = new DOMDocument(); 

// It's rare you'll have valid XHTML, suppress any errors- it'll do its best. 
@$doc->loadhtml($string); 

$xpath = new DOMXPath($doc); 

// Modify the XPath query to match the content 
foreach($xpath->query('//table')->item(0)->getElementsByTagName('tr') as $rows) { 
    $cells = $rows->getElementsByTagName('td'); 

    // Do stuff with the data 
    echo $cells->item(0)->textContent; 
    echo $cells->item(1)->textContent; 
    echo $cells->item(2)->textContent; 

This answer在SO上也會有所幫助。它非常詳細地解釋了從HTML表格中提取信息的最佳方法,使用PHP