php xpath使用类获取div的内容 [英] php xpath get contents of div with class

查看:142
本文介绍了php xpath使用类获取div的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用xpath获取具有特定类的所有div内容的正确语法是什么?我似乎正在使用div,但我不知道如何获取其innerHTML。

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";
    }


推荐答案

class = page-display divs-因此您将一无所获。

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

您是说get class = parbase cn_text吗?

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/>";
    }

这篇关于php xpath使用类获取div的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆