zoukankan      html  css  js  c++  java
  • 采集练习(十一) php 获得电视节目预告---数据来自电视猫

      昨天写了个采集搜视网的电视节目预告,刚好今天有心情,想采下其他网站提供的节目预告,发现  电视猫wap版 的提供的节目预告也蛮好采(需要正则)....感谢移动互联网!

    电视猫的 wap版地址是 http://wap.tvmao.com/  点击相应的电视台 进去就能看到 相应的 节目预告

    如:http://wap.tvmao.com/cctv.jsp  里的就是 央视的 相应频道列表  点击 相应的 频道 就可以看到  该频道的 的节目预告;

    http://wap.tvmao.com/program.jsp?p=CCTV&c=CCTV1&w=6  就是 CCTV-1 周六 的节目预告 。

    分析 页面html 得知   /program.jsp?p=CCTV&c=CCTV1 来自  http://wap.tvmao.com/cctv.jsp 页面的频道列表里  而 w=6 表示 周六

    下面是采集央视的测试代码:

    <?php
    /**
     * Created by JetBrains PhpStorm.
     * User: keygle
     * Date: 13-8-3
     * Time: 下午2:04
     * From  www.cnblogs.com/keygle
     */
    
    /**
     * [curl 带重试次数]
     * @param  [type]  $url     [访问的url]
     * @param  [type]  $post    [$POST参数]
     * @param  integer $retries [curl重试次数]
     * @return [type]           [description]
     */
    function curlGetHtml($url, $post = null, $retries = 3){
        $ch = curl_init();
        if(is_resource($ch) === true){
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_FAILONERROR, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_REFERER, "http://wap.tvmao.com/");
            curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
            if(isset($post) === true){
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, (is_array($post) === true) ? http_build_query($post, "", "&"): $post);
            }
            $result = false;
            while(($result === false) && (--$retries > 0)){
                $result = curl_exec($ch);
            }
            curl_close($ch);
        }
        return $result;
    }
    
    /**
     * [getTvUrl 获得电视台链接数组]
     * @param  [type] $tvListHtml [页面html ]
     * @return [type]             [description]
     */
    function getTvUrl($tvListHtml){
        $tvListArray = array();
        //正则匹配 url 和 电视台名
        preg_match_all('#<a href="/([^"]+)">(.*?)</a><br/>#i', $tvListHtml, $matches);
        foreach ($matches[1] as $key => $value) {
            $tvListArray[$key]['url'] ="http://wap.tvmao.com/".html_entity_decode($value); //html 实体转换
            $tvListArray[$key]['name'] = $matches[2][$key];
        }
        return $tvListArray;
    }
    
    /**
     * [getPlayItems 获得电视节目预告]
     * @param  [type] $tvUrl [description]
     * @return [type]        [description]
     */
    function getPlayItems($tvUrl){
        $playItems = array();
        $itemHtml = curlGetHtml($tvUrl);
        preg_match_all("#r/>([^<]+)?<b#i", $itemHtml, $matches);
        array_shift($matches[1]); //去掉数组的第一个
        $playItems = $matches[1];
        return $playItems;
    }
    
    //获得央视 的所有频道
    $url = "http://wap.tvmao.com/cctv.jsp";
    $tvListHtml  = curlGetHtml($url);
    $tvListArray = getTvUrl($tvListHtml);
    print_r($tvListArray);
    // 获得cctv1的 周六 节目预告
    $tvUrl = "http://wap.tvmao.com/program.jsp?p=CCTV&c=CCTV1&w=6";
    $playItems = getPlayItems($tvUrl);
    print_r($playItems);
  • 相关阅读:
    Xshell6提示“要继续使用此程序,您必须应用最新的更新或使用新版本”的解决办法
    设置UIImage的边框和圆角大小以及颜色
    CATransition使用
    [转]C/C++中extern关键字详解
    New ipad与ipad2有何不同
    使用Xcode收藏自己常用的代码模板
    objectivec 关于 self 的用法总结
    查找 EXC_BAD_ACCESS 问题根源的方法
    结构体的对齐方式
    添加three20模板的方法
  • 原文地址:https://www.cnblogs.com/keygle/p/3235105.html
Copyright © 2011-2022 走看看