Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php xpath get contents of div with class

What is the right syntax to use xpath to get the contents of all divs with a certain class? i seem to be getting the divs but i don't know how to get their innerHTML.

        $url = "http://www.vanityfair.com/politics/2012/10/michael-lewis-profile-barack-obama";

    $ctx     = stream_context_create(array('http'=> array('timeout' => 10)));

    libxml_use_internal_errors(TRUE);
    $num = 0;

    if($html = @file_get_contents($url,false,$ctx)){

        $doc   = DOMDocument::loadHTML($html);
        $xpath = new DOMXPath($doc);

        foreach($xpath->query('//div[@class="page-display"]') as $div){
            $num++;
            echo "$num. ";

            //????

            echo "<br/>";
        }

        echo "<br/>FINISHED";

    }else{
        echo "FAIL";
    }
like image 266
David Avatar asked Sep 20 '12 23:09

David


1 Answers

There is no HTML in the class="page-display" divs - so you're not going to get anything at all.

Do you mean the get class="parbase cn_text"?

    foreach($xpath->query('//div[@class="parbase cn_text"]') as $div){
        $num++;
        echo "$num. ";

        //????
        echo $div->textContent;

        echo "<br/>";
    }
like image 111
Robbie Avatar answered Nov 19 '22 17:11

Robbie