我想從這個網站報廢數據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