Curl写请求盗取金十网首页数据,附thinkphp源码包

浏览:2847 发布日期:2016/09/14 分类:系统代码 关键字: php curl 正则
玩了curl后发现php正则真的很重要,可惜我很菜。拿了网页上的三个字段写了个demo。下面贴的代码复制下来可以直接运行。其实就是用别人的数据自己做API的一种思路。
希望大家多多支持我在看云发布的手册,第一章对登录讲解的超级详细。http://www.kancloud.cn/w113211/killall《基于ThinkPHP5.0的各项后台小功能代码实现》
[/img]
ThinkPhP两个版本的权限管理(3.2.3和5.0)
https://www.kancloud.cn/w113211/alls
<?php 
function getwebcontent($url){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
    $contents = trim(curl_exec($ch)); 
    curl_close($ch); 
    return $contents; 

function DeleteHtml($str) { 
     $str = trim($str); 
     $str = str_replace("\t","",$str); 
     $str = str_replace("\r\n","",$str); 
     $str = str_replace("\r","",$str); 
     $str = str_replace("\n","",$str); 
     return trim($str); 
 }
     set_time_limit(0);
    $datas =DeleteHtml(getwebcontent('http://www.jin10.com')); 
    preg_match_all('/(\d{2}:\d{2})/',$datas,$time);  /*对应的所有时间*/
    preg_match_all('/<td[^>]+?id\s*?=\s*?"content_\d+?">(.+?)<\/td>/',$datas,$content);  /*对应的所有内容*/
    preg_match_all('/"普通新闻"|"重要新闻"|"一般数据"|"重要数据"/',$datas,$importance);  /*对应的所有新闻的重要性*/
    $contents=$content[1];           /*对应的所有内容*/
    $times=$time[1];                  /*对应的所有时间*/
    $importances=$importance[0];

    $news=[];
    foreach ($contents as $key => $value) {
        $t=[
            "content"=>$value,
            "time"=>$times[$key],
            "importance"=>str_replace('"','',$importances[$key]),
        ];
        array_push($news,$t);
    }
    var_dump($news);

 ?>

附件 109news.rar ( 1.12 MB 下载:54 次 )

评论( 相关
后面还有条评论,点击查看>>