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>";
        }
    }
    ?>

  • 相关阅读:
    2020.10.23 19级training 补题报告
    2020.10.17 天梯赛练习 补题报告
    2020.10.16 19级training 补题报告
    2020.10.9 19级training 补题报告
    2020.10.10 天梯赛练习 补题报告
    2020.10.3 天梯赛练习 补题报告
    2020.10.2 19级training 补题报告
    第十届山东省ACM省赛复现补题报告
    VVDI Key Tool Plus Adds VW Passat 2015 Key via OBD
    Xhorse VVDI Prog Software V5.0.3 Adds Many MCUs
  • 原文地址:https://www.cnblogs.com/see7di/p/2239686.html
Copyright © 2011-2022 走看看