duanaoshu1989 2014-05-19 04:49
浏览 40
已采纳

使用DOM获取HTML代码。

I am collecting username, title, video count and comment count from html source. Consider this link

Here is my code to get it:

function getParameter($url)
{
    $html = file_get_html($url);
    if($html)
    {
            $containers1 = $html->find('div.v div.v-link 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 = $html->find('div.v div.v-link v-meta va a'); //get user name
            foreach($containers2 as $username)
            {
                $user = $username->plaintext;
            }
             $containers3 = $html->find('div.v div.v-link a'); //get video title
            foreach($containers3 as $title)
            {
                $title = $title->plaintext;
            }
            $commentcontainers = $html->find('div.ico-statcomment span'); //get nos of comments
            foreach($commentcontainer as $cont)
            {
                $comments = $cont->plaintext;
            }
            return $data;               
    }
}

But it gives this error:

Warning: Invalid argument supplied for foreach() in /var/www/html/scrap/yelp/yoku.php on line 41 

any hints for this?

Here is the source code snippet:

<div class="v" >

    <div class="v-thumb">
        <img src="http://r1.ykimg.com/0515000052B7B3D46714C0318106EA36" alt="和小姚晨约会激动抽筋" />
                            <div class="v-thumb-tagrb"><span class="v-time">08:56</span></div>
            </div>
    <div class="v-link">
        <a href="http://v.youku.com/v_show/id_XNjQ0MTAyMzQ0.html" target="video" title="和小姚晨约会激动抽筋"></a>
    </div>
        <div class="v-meta va">
                            <div class="v-meta-neck">
                        <a class="v-useravatar" href="http://i.youku.com/u/UMTQxNDg3NzA4"  target="_blank"><img title="嘻哈四重奏" src="http://g3.ykimg.com/0130391F455211D40E180C021BBB97D37C4057-26F4-2F53-A8CD-4A4139415701"></a>
                        <span class="v-status">&nbsp;</span>
                        <a class="v-username" href="http://i.youku.com/u/UMTQxNDg3NzA4"  target="_blank">嘻哈四重奏</a>
                    </div>
                        <div class="v-meta-title"><a href="http://v.youku.com/v_show/id_XNjQ0MTAyMzQ0.html" target="video">和小姚晨约会激动抽筋</a></div>
                                <div class="v-meta-entry">
            <i class="ico-statplay" title="播放"></i><span class="v-num">588万</span>&nbsp;<i title="评论" class="ico-statcomment"></i><span class="v-num">1,290</span>      </div>
                        <div class="v-meta-overlay"></div>
            </div>
    </div>
  • 写回答

1条回答 默认 最新

  • douluo3256 2014-05-19 05:19
    关注

    Check selectors, documentation here simplehtmldom. For example, I did some changes in selectors.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分