zoukankan      html  css  js  c++  java
  • 用PHP的CURL写的一个采集Discuz的例子

    以前写ASP的时候,要实现采集功能,那简直就是一个浩浩荡荡的大工程,现在用PHP简单多了,轻轻松松简简单单就能把ASP长篇大论才能实现的功能搞定.
    这是我用PHP的CURL写的一个采集Discuz的例子,附带模拟登陆,如果不需要模拟登陆就可以直接用File_Get_Contents来采那会更简单.
    <?php
    set_time_limit(0);

    //cookie保存目录
    $cookdir = './cookie.tmp';

    //模拟请求数据
    Function request($url,$action,$cookdir,$referer){
        $ch = curl_init();
        $options = array(CURLOPT_URL => $url,
            CURLOPT_HEADER => 0,
            CURLOPT_NOBODY => 0,
            CURLOPT_PORT => 80,
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => $action,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_FOLLOWLOCATION => 1,
            CURLOPT_COOKIEJAR => $cookdir,
            CURLOPT_COOKIEFILE => $cookdir,
            CURLOPT_REFERER => $referer
        );
        curl_setopt_array($ch, $options);
        $code = curl_exec($ch);
        curl_close($ch);
        return $code;
    }

    //获取帖子列表
    Function getList($code){
        //<a href="viewthread.php?tid=16&extra=page%3D1">加打塔洗花腳本</a>
        preg_match_all('/<a href=\"viewthread.php\?tid=(\d+)/',$code,$threads);
        return $threads[1];
    }

    //判断该帖子是否存在
    Function isExits($code){
        preg_match('/<p>指定的主题不存在或已被删除或正在被审核,请返回。<\/p>/',$code,$error);
        return isset($error[0])?false:true;
    }

    //获取帖子标题
    Function getTitle($code){
        preg_match('/<h1>[^<\/h1>]*/',$code,$title_tmp);
        $title = $title_tmp[0];
        return $title;
    }


    //登录论坛/
    $url = 'http://www.175club.com/logging.php?action=login';
    $action='loginfield=username&username=see7di&password=fjin999&questionid=0&cookietime=315360000&referer=http://bbs.war3.cn/&loginsubmit=提交';
    request($url,$action,$cookdir,'');
    unset($action,$url);

    //获取帖子列表(位于第一页的帖子)
    $url = 'http://www.175club.com/forumdisplay.php?fid=5';
    $code = request($url,'',$cookdir,'');
    $tList = getList($code);

    //循环抓取每個帖子內的標題
    foreach($tList as $list){
        $url = "http://www.175club.com/viewthread.php?tid={$list}";
        $code = request($url,'',$cookdir,'');
        if(isExits($code)){
            $title = getTitle($code);
            echo "tid:{$list}:",strip_tags($title),"<br>";
        }else{
            echo "tid:{$list}:该帖子不存在!<br>";
        }
    }
    ?>

  • 相关阅读:
    java堆
    本地方法栈
    java虚拟机栈
    Java 程序计数器
    面向对象 基本概念 复习
    if __name__=='__main__'
    偏函数与模块
    可变参数与关键字参数(复习材料)
    匿名函数
    闭包
  • 原文地址:https://www.cnblogs.com/see7di/p/2239686.html
Copyright © 2011-2022 走看看