我怎么用ph p find函数找到这个“td”?

问题描述 投票:0回答:2

这是我的代码:

    $post = [
        'iatacode' => 'DME',
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.airlinecodes.co.uk/aptcoderes.asp');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
    $response = curl_exec($ch);
    var_export($response);
    $html5 = str_get_html($response);
    $elem = $html5->find('td', 5);
    echo $elem;

改变我的问题,因为它的一半答案:我如何只获得第5个“td”并且不显示整个卷曲的页面?

如何在页面上只显示第6个“td”并“显示”?我不希望其余的显示......

php html curl web-scraping
2个回答
0
投票

如果你删除'var_export($ response);'你只会看到你选择的'td'

<?php

$post = [
        'iatacode' => 'DME',
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.airlinecodes.co.uk/aptcoderes.asp');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
    $response = curl_exec($ch);
    //var_export($response);
    $html5 = str_get_html($response);
    $elem = $html5->find('td', 5);
    echo $elem;

?>

0
投票

显示第6个td:

$response = curl_exec($ch);
echo (@DOMDocument::loadHTML ( $response ))->getElementsByTagName ( "td" )->item ( 6 - 1 )->textContent;

(-1是因为它从0开始计数,因此第6项实际上是td#7)

© www.soinside.com 2019 - 2024. All rights reserved.