采集练习(十一) php 获得电视节目预告---数据来自电视猫
2024-08-29 07:53:12
昨天写了个采集搜视网的电视节目预告,刚好今天有心情,想采下其他网站提供的节目预告,发现 电视猫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);
最新文章
- MongoDB管理工具的插件系统
- Search and Replace的正则表达式
- mysql 命令行参数
- windows防火墙添加规则
- IIS报错,App_global.asax.&#215;&#215;&#215;.dll拒绝访问
- Qt自绘窗体
- android createbitmap函数内存溢出,求解怎样进行处理out of memory溢出问题
- 【JSP动态网站】JDBC连接SqlServer 2008数据库
- RemoveAll 要重写equals方法
- The Suspects(并查集求节点数)
- Extjs 4.1 struts2.3 返回json 初试
- Linux 修改本地时间 (centos为例)
- ASP.NET之使用Ajax实现页面异步刷新(无需刷新整个页面)
- ORM作业
- Spring Batch 简介
- ALGO-14_蓝桥杯_算法训练_回文数
- es6(12)--类,对象
- 收藏一篇关于Asp.net Response.Filter的文章
- [PLC]ST语言三:OUT/OUT_T/OUT_C/OUT_C-C32
- appium镜像设置