为不同的块分别回显html标签值 [英] echo html tag value separately for different block

查看:75
本文介绍了为不同的块分别回显html标签值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was trying to get username, title, comment count and video count from html source code. There are few blocks which each contains different details for this parameters. Here is code which scrap data but issue is it scrap all of them in list.

首先像所有视频计数,然后是所有注释计数,依此类推。不能为每个块单独。 请考虑以下链接

Like first all video count, then all comments count and so on. Not for each block separate. Consider this link for source code.

这是代码:

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'); 。它给出为foreach()提供的 Invalid参数。如果有人告诉我问题出在哪里,我将不胜感激。

also there is issue at $commentcontainers = $div->find('div[class=v-meta va] div.v-meta-entry span');. It gives Invalid argument supplied for foreach(). I appreciate help if someone tell me where is issue

推荐答案

我测试了此功能,输出:

I tested this function, output:

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

    }
}

这篇关于为不同的块分别回显html标签值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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