如何在php中获取节点的子节点?

时间:2012-09-04 13:38:06

标签: php curl

我需要获取我访问过的特定节点的子节点(带有class = title的li节点)

请帮我怎么做?

    <?php
    $options = array(
    CURLOPT_RETURNTRANSFER => true,     // return web page
    CURLOPT_HEADER         => false,    // don't return headers
    CURLOPT_FOLLOWLOCATION => true,     // follow redirects
    CURLOPT_ENCODING       => "",       // handle all encodings
    CURLOPT_USERAGENT      => "SomeUcam v0.1 Bot", // who am i
    CURLOPT_AUTOREFERER    => true,     // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
    CURLOPT_TIMEOUT        => 120,      // timeout on response
    CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
       );

        $ch      = curl_init( "http://example.com/?page=1" );
       curl_setopt_array( $ch, $options );
       $content = curl_exec( $ch );
       $err     = curl_errno( $ch );
       $errmsg  = curl_error( $ch );
       $header  = curl_getinfo( $ch );
       curl_close( $ch );

    $newDom = new domDocument;
    $newDom->loadHTML($content);

    $finder = new DomXPath($newDom);
    $classname="title";
    $nodes = $finder->query("//*[contains(@class, '$classname')]");
    $nodesNo = $nodes->length;
    echo $nodesNo;

    ?>

1 个答案:

答案 0 :(得分:0)

例如,您可以使用foreach循环迭代节点:

$url = 'http://example.com/';

/* this is necessary to prevent DOMDocument errors on HTML5-elements */
libxml_use_internal_errors( true );

$dom = new DOMDocument();
$dom->loadHTMLFile( $url );
$finder = new DOMXpath( $dom );
$nodes = $finder->query( "//*[contains(@class, 'title')]" );

if( $nodes instanceof DOMNodeList )
{
    foreach( $nodes as $node )
    {
        var_dump( $node->nodeName, $node->nodeValue );
    }
}