【问题标题】:PHP Web Scraping from table HTML tags [closed]从表格 HTML 标记中进行 PHP Web Scraping [关闭]
【发布时间】:2016-08-20 20:46:55
【问题描述】:

我正在查看从网站、表格中抓取数据,并在 PHP 中显示在干净的表格中。

网站示例如下,您会注意到航班数据表。关于如何让 PHP 循环数据并将其放入表中的任何想法?

Data Example

【问题讨论】:

    标签: php html mysql curl


    【解决方案1】:

    是的,我建议使用 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);
        }
        ?>
    

    【讨论】:

    • 这真是太好了。我将如何拉回位于航班号和“实时”链接后面的 URL?
    【解决方案2】:

    与任何抓取工作一样,请记住,您可能违反了他们的服务条款,尤其是在您重新发布内容时。话虽如此,https://github.com/FriendsOfPHP/Goutte 非常适合抓取此类任务。

    <?php
    require 'vendor/autoload.php';
    use Goutte\Client;
    
    $data_url = 'https://www.flightradar24.com/data/flights/southwest-airlines-wn-swa';
    $client = new Client();
    $crawler = $client->request('GET', $data_url);
    $crawler->filter('#tbl-datatable')->each(function ($node) {
        print $node->html()."\n";
    });
    

    【讨论】:

    • 数据没有被重新发布。这是供个人使用的。此外,我们为网站支付了大笔订阅费用,该网站允许使用各种方法离线访问数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 2016-06-03
    • 2018-08-06
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    相关资源
    最近更新 更多