为不同的块分别回显html标记值

时间:2014-05-19 05:59:12

标签: php dom

我试图从html源代码获取用户名,标题,评论数和视频数。有几个块,每个块包含此参数的不同详细信息。以下是废弃数据的代码,但问题是废弃列表中的所有内容。

首先是所有视频计数,然后所有评论计数等等。不是每个块分开。 Consider this link获取源代码。

这是代码:

function getParameter($url)
{
    $html = file_get_html($url);
    if($html)
    {   
        //we iterate all 'div.v' and select data from every 'div.v' separately
        $containersDiv = $html->find('div.v'); 
        foreach($containersDiv as $div) 
        {
            $containers1 = $div->find('div[class=v-meta va] div.v-meta-entry'); 
            foreach($containers1 as $container)
            {               
                $plays = $container->find('.v-num'); // get nos of time video played
                $item = new stdClass();
                foreach($plays as $play)
                {
                    $nos = $play->plaintext; 
                }
                //echo $address;
            }
             $containers2 = $div->find('div[class=v-meta va] a'); //get user name
            foreach($containers2 as $username)
            {
                $user = $username->plaintext;
            }
             $containers3 = $div->find('div.v-link a'); //get video title
            foreach($containers3 as $title)
            {
                $title = $title->plaintext;
            }
            $commentcontainers = $div->find('div[class=v-meta va] div.v-meta-entry span'); //get nos of comments changed
            foreach($commentcontainer as $cont)
            {
                $comments = $cont->plaintext;
            }
        }

        return $data;               
    }
}

$commentcontainers = $div->find('div[class=v-meta va] div.v-meta-entry span');也存在问题。它给出了Invalid argument supplied for foreach()。如果有人告诉我问题在哪里,我感谢你的帮助

1 个答案:

答案 0 :(得分:1)

我测试了这个功能,输出:

new div -------------------

450万 
Mini剧-乙方甲方
我还以为你要抢鸡蛋呢
843 
new div -------------------

134万 
万万没想到
<万万没想到>雪藏篇
470 
new div -------------------

236万 
曾经想火
闺蜜的情人竟是我老板
422 
new div -------------------

641万 
暴走漫画
日版“周董”来华拍电影
3,959 
new div -------------------

695万 
Mini剧-乙方甲方
<乙方甲方>唐僧爱上90后
1,242 
new div -------------------

function getParameter($url)
{

    $html = file_get_html($url);

    if($html)
    {   

        //we iterate all 'div.v' and select data from every 'div.v' separately
        $containersDiv = $html->find('div.v'); 
        foreach($containersDiv as $div) 
        {
            echo "new div -------------------</br></br>";
            $timevideo = $div->find('div[class=v-meta va] div.v-meta-entry span', 0); 
            $nos = $timevideo->plaintext; 
            echo $nos."</br>";

            $containers2 = $div->find('div[class=v-meta va] a.v-username', 0); //get user name
            $user = $containers2->plaintext;
            echo $user."</br>";

            $containers3 = $div->find('div.v-link a', 0); //get video title
            $title = $containers3->title;
            echo $title."</br>";

            $comments = $div->find('div[class=v-meta va] div.v-meta-entry span', 1); 
            $comments_count = $comments->plaintext; // comments count
            echo $comments_count."</br>";
        }

    }
}