doubihuai8468 2014-05-19 05:59
浏览 7
已采纳

为不同的块分别回显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.

here is the 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;               
    }
}

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

  • 写回答

1条回答 默认 最新

  • doutang1992 2014-05-19 06:39
    关注

    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>";
            }
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题