使用表格将网站抓取到数组

时间:2016-08-31 13:06:20

标签: php html

有人可以帮忙吗。

我想用一张桌子刮一个网站。

表格标题是

调度 从, 飞行, 状态, 终端,

然后我想将其输出到数组。

然后我想搜索输出数据的数组到文本框中。

任何人都可以帮忙。

非常感谢提前

我试过尝试下面的

<?php

$htmlContent = file_get_contents("http://www.gatwickairport.com/flights/");

$DOM = new DOMDocument();
$DOM->loadHTML($htmlContent);

$Header = $DOM->getElementsByTagName('th');
$Detail = $DOM->getElementsByTagName('td');

//#Get header name of the table
foreach($Header as $NodeHeader) 
{
    $aDataTableHeaderHTML[] = trim($NodeHeader->textContent);
}
//print_r($aDataTableHeaderHTML); die();

//#Get row data/detail table without header name as key
$i = 0;
$j = 0;
foreach($Detail as $sNodeDetail) 
{
    $aDataTableDetailHTML[$j][] = trim($sNodeDetail->textContent);
    $i = $i + 1;
    $j = $i % count($aDataTableHeaderHTML) == 0 ? $j + 1 : $j;
}
//print_r($aDataTableDetailHTML); die();

//#Get row data/detail table with header name as key and outer array index as row number
for($i = 0; $i < count($aDataTableDetailHTML); $i++)
{
    for($j = 0; $j < count($aDataTableHeaderHTML); $j++)
    {
        $aTempData[$i][$aDataTableHeaderHTML[$j]] = $aDataTableDetailHTML[$i][$j];
    }
}
$aDataTableDetailHTML = $aTempData; unset($aTempData);
print_r($aDataTableDetailHTML); die();
?>

但我需要它只做1,2,3,4,5列而不是0,6,7,8,9

非常感谢提前

1 个答案:

答案 0 :(得分:1)

您可以尝试这些行 - 您可以在其中确定要保留的列索引并使用in_array进行测试

$cols=array(1,2,3,4,5);
for($i = 0; $i < count($aDataTableDetailHTML); $i++){
    for($j = 0; $j < count($aDataTableHeaderHTML); $j++){
        if( in_array( $j, $cols ) ) $aTempData[$i][$aDataTableHeaderHTML[$j]] = $aDataTableDetailHTML[$i][$j];
    }
}

要回答您的后续问题,我希望以下内容对您有用。 (尝试运行它&#34;原样&#34;查看结果)

$url='http://www.gatwickairport.com/flights/';
$html=file_get_contents( $url );


if( $html ){

    $dom=new DOMDocument;
    $dom->loadHTML( $html );
    $xp=new DOMXPath( $dom );


    $headers=array();
    $data=array();
    $cols=array(1,2,3,4,5);


    /* Get column headers */
    $col=$xp->query( '//div[@class="container"]/table/thead/tr/th' );

    /* Get & store the number of column headers */
    $length=$col->length;

    /* Iterate through headers and store the ones you wish to keep */
    if( !empty( $col ) ){
        foreach( $col as $i => $node ) {
            /* array/collection is zero based so we need one more as the index */
            $j=$i+1;
            if( $node->tagName=='th' && in_array( $j, $cols ) ) $headers[]=trim( $node->nodeValue );
        }
    }

    /* Get row data */
    $col=$xp->query( '//div[@class="container"]/table/tbody/tr/td' );
    $rows=$col->length;

    /* each row will have $length columns, there will be $rows rows */
    $j=1;
    $r=1;

    if( !empty( $col ) ){
        foreach( $col as $i => $node ) {

            if( $node->tagName=='td' && in_array( $j, $cols ) ) {
                /* Because the airline column displays an image, treat it differently */
                if( $j==1 && $node->hasChildNodes() && $node->childNodes->item(0)->tagName=='img' ){
                    $value=$node->childNodes->item(0)->getAttribute('alt');
                } else {
                    /* standard cell content */
                    $value=trim( $node->nodeValue );
                }

                /* Add new row/cell data to output */
                $data[ 'row_'.$r ][]=$value;
            }

            if( $j >= $length ) {
                $j=0;
                $r++;
            }
            $j++;
        }
    }


    /* do stuff with the output data ( 2 arrays ) */
    echo '<pre>',print_r($headers,true),'</pre>';
    echo '<pre>',print_r($data,true),'</pre>'; 
}