从表格HTML标记进行PHP Web抓取 [英] PHP Web Scraping from table HTML tags
本文介绍了从表格HTML标记进行PHP Web抓取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在查看从网站,表中抓取数据,并显示在PHP的干净表中.
I'm looking at scraping data from a website, from a table, and display in a clean table in PHP.
下面的网站示例,您会注意到航班数据表.关于如何使PHP遍历数据并将其放入表的任何想法吗?
Website example is below, you will notice the table of flight data. Any idea on how I can get PHP to loop over the data and place it into a table?
推荐答案
是的,我建议使用Xpath
Yes, i would recommend using Xpath
<h1>This is scraping flight radar:</h1>
<?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='tbl-datatable']/thead/tr/th[position()>1]") as $header)
{
$newRow->appendChild($result->createElement("th", trim($header->nodeValue)));
}
foreach($xpath->query("//table[@id='tbl-datatable']/tbody/tr") as $row)
{
$newRow = $tbody->appendChild($result->createElement("tr"));
foreach($xpath->query("./td[position()>1 and position()<7]", $row) as $cell)
{
$newRow->appendChild($result->createElement("td", trim($cell->nodeValue)));
}
}
echo $result->saveXML($result->documentElement);
}
?>
这篇关于从表格HTML标记进行PHP Web抓取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文