2017-04-24 54 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