我想从这个网站报废数据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)));
}
}
}
我将不胜感激为其提供任何帮助,谢谢
本网站不是为您的项目获得免费工作人员而设计的。请告诉我们你已经尝试了什么。 – Peter
这很公平,好吧,我正在看循环中的这个区域 – Bels
你在最后@Bels做了什么? – Lissy