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

      前几天逛湖南卫视,偶然间发现它的网站上也有节目预告,一看源码,居然是来自搜视网的xml,于是就想获得它的数据(页面直接ajax加载估计会有跨域问题)

    前阵子也写过另一个方法获得 节目预告(采集练习(七) php 获得电视节目预告)。

    点开 金鹰网  导航上找到 电视剧  鼠标移上 电视剧 点击里面的 节目表 其实就 是这个链接 http://hunantv.tvsou.com/  数据来自搜视网

    在页面上点击 湖南卫视  发现有2个 ajax 请求  

    1. XHR finished loading: http://hunantv.tvsou.com/pst/tv_24/ch_46/W6.xml  里面是 是 湖南卫视 周六的 节目预告
    1. XHR finished loading: http://hunantv.tvsou.com/xml/ws.xml  里面是地方卫视的xml列表

    通过分析 http://hunantv.tvsou.com/xml/ws.xml 发现  tv_24 的 24 是 湖南卫视的id ;

    ch_4646 是湖南卫视的 tvid ,而W6   则是湖南卫视 周六的 节目预告

    根据电视台的 xml 就可以获得相应的 节目预告了

    其余电视台 节目预告也类似

    遇到的问题: 相应电视台的 id 在 xml 节点的属性上,以前都是直接用 DOMDocument 类来读取,这次尝试用 simplexml 类来处理。

    下面是我拿央视做测试的代码 :

    <?php
    /**
     * Created by JetBrains PhpStorm.
     * User: keygle
     * Date: 13-8-2
     * Time: 下午9:55
     * From: www.cnblogs.com/keygle
     */
    
    /**
     * [getXml 读取xml]
     * @param  [type] $url [xml url]
     * @return [type]      [description]
     */
    function getXml($url){
    	$xmlStr = @file_get_contents($url);
    	$xmlObj = @simplexml_load_string($xmlStr);
    	return $xmlObj;
    }
    
    /**
     * [generateTvArray 生成电视列表数组]
     * @param  [type] $tvUrl [xml 地址]
     * @return [type] $datas    [电视列表数组]
     */
    function generateTvArray($tvUrl){
    	$tvObject = getXml($tvUrl);	
    	$datas = array();
    	//获得Tv节点
    	$tvNode = $tvObject->Tv;
    	//遍历节点 
    	for($i=0;$i<$tvNode->count();$i++){
    		//获得节点属性 重组数组
    		foreach ($tvNode[$i]->attributes() as $k=>$v){
    			$datas[$i][$k] = (string)$v; //sim 对象转为 string 
    			$channelNode = $tvNode[$i]->Channel;
    			for($n=0;$n<$channelNode->count();$n++){
    				$datas[$i]['channel'][$n]['name'] = strval($channelNode[$n]);
    				foreach ($channelNode[$n]->attributes() as $key => $value) {
    					$datas[$i]['channel'][$n][$key] = strval($value);
    				}
    			}
    		}
    	}
    	return $datas;
    }
    /**
     * [getPlayItems 获得节目预告]
     * @param  [type] $playItemsUrl [xml 地址]
     * @return [type]               [array]
     */
    function getPlayItems($playItemsUrl){
    	$playItemsObject = getXml($playItemsUrl); 
    	return json_decode(json_encode($playItemsObject),true); //将 simplexml 对象转为数组
    }
    //获得央视的 频道列表
    $tvUrl = "http://hunantv.tvsou.com/xml/ys.xml"; 
    $tvListArray = generateTvArray($tvUrl);
    print_r($tvListArray);
    //获得 CCTV1 周六的节目预告
    $playItemsUrl = "http://hunantv.tvsou.com/pst/tv_1/ch_1/W6.xml";
    $playItemsArray = getPlayItems($playItemsUrl);
    print_r($playItemsArray);
    
    
  • 相关阅读:
    hlgoj 1766 Cubing
    Reverse Linked List
    String to Integer
    Bitwise AND of Numbers Range
    Best Time to Buy and Sell Stock III
    First Missing Positive
    Permutation Sequence
    Next Permutation
    Gray Code
    Number of Islands
  • 原文地址:https://www.cnblogs.com/keygle/p/3234130.html
Copyright © 2011-2022 走看看