Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为不同的块分别回显html标记值_Php_Dom - Fatal编程技术网

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

Php 为不同的块分别回显html标记值,php,dom,Php,Dom,我试图从html源代码中获取用户名、标题、评论数和视频数。很少有块包含此参数的不同详细信息。这是一个代码,它可以删除数据,但问题是它会删除列表中的所有数据 像首先所有视频计数,然后所有评论计数等等。不是每个街区都分开的。对于源代码 代码如下: function getParameter($url) { $html = file_get_html($url); if($html) { //we iterate all 'div.v' and selec

我试图从html源代码中获取用户名、标题、评论数和视频数。很少有块包含此参数的不同详细信息。这是一个代码,它可以删除数据,但问题是它会删除列表中的所有数据

像首先所有视频计数,然后所有评论计数等等。不是每个街区都分开的。对于源代码

代码如下:

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提供的无效参数。如果有人告诉我问题在哪里,我将不胜感激。我测试了此功能,输出:

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 -------------------

$div->查找'div.v-link a',0;这里0表示什么?这是数组中的数字元素、0-第一个元素、1-第二个元素,等等。其中包含的标记很少。
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>";
        }

    }
}