使用PHP 的 curl 爬取 青果教务系统 课表

来源:互联网 发布:火电经济评价软件 编辑:程序博客网 时间:2024/05/03 20:28

近段需要写关于爬取课表的功能,现在完成了,来总结一下,说说自己踩过的坑。希望能帮到大家。

1. 分析

首先我们要了解 Http Cookie 的作用(可参考HTTP cookies 详解),简单来说就是维持一个会话,这样我们就能在登陆一个网页后,就能进入这个网页需要登陆的界面。

现在我们需要模拟登陆青果教务系统,就也需要先获取服务器给我们的cookie,然后用这个cookie值去获取验证码登陆,获取我们想要的内容。要注意的是有的网站对表头信息也是有验证的,我们需要在请求中添加表头信息。

总结起来就三部,首先获取登录界面的验证码并存储Cookie,然后通过cookie来模拟登陆,最后进入教务系统取想要的东西。

现在我们需要去留意的内容,各个请求的连接、header、和发送的数据

2. 查看请求

首先我们查看首页,我们发现登录并不在首页上,需要点击用户登录后才算进入了登录界面。
这里写图片描述

这里写图片描述

然后我们查看登录界面的请求。我们就是需要图中的Cookie 来登录,
这里写图片描述

在看验证码的请求,发现其中你的Cookie是一样的,所以,我们直接获取验证码的Cookie保存就行,不管登界面。
这里写图片描述

我们在看登录的请求,请求类型为POST,还是原来的cookie,但是我们发现传送的数据竟然那么多,其实,比多请求几次就会发现,其实有几个的值是永远不会变的,我们下面接着分析。
这里写图片描述
这里写图片描述

查看表单登录结构,发现刚才的提交数据都是隐藏的标签,并且都是大部分都是固定值,只有两个是我在输入密码或验证码时会一直变动,其实这两个就是密码和验证码,只是进行了特殊处理,这个网页引入了一个 md5.js 文件(上面第二张图中可以看到),加密就是通过这个文件进行的。
这里写图片描述

我们寻找加密部分的代码,在页面的某一部分,我们发现了加密的代码,我们在模拟登陆时就可以使用这部分处理了。
这里写图片描述

当我们登陆成功后我们课表的请求。OK,我们的心思收集工作完成了。下面开始编码吧。
这里写图片描述

3. 获取验证码和Cookie

首先我们需要一个界面来模拟登陆,我写了一个简单的html form登录,需要注意的是咋提交账号密码时要对信息使用 md5.js 加密。

/**  *test.html文件  */<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport"          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">    <title>课表登录</title></head><body><form action="./php/login.php" method="post">    <div class="form-group">        <input type="hidden" name="password" value="" id="password">        <input type="hidden" name="validate" value="" id="validate">    </div>    <div class="form-group">        <label for="id">学号:</label>        <input type="number" class="form-control" id="id" name="txt_asmcdefsddsd" placeholder="" onblur="chkpwd()" onkeyup="chkpwd()">    </div>    <div class="form-group">        <label for="exampleInputPassword1">密码:</label>        <input type="password" class="form-control" id="exampleInputPassword1"  name="txt_pewerwedsdfsdff" placeholder="Password" onblur="chkpwd()" onkeyup="chkpwd()">    </div>    <div class="form-group">        <label for="exampleInputValidate">验证码:</label>        <input type="text" class="form-control" id="exampleInputValidate" name="validatessss" placeholder="" onblur="chkyzm()" onkeyup="chkyzm()">        <img src="./php/getValidate.php" onclick="changeValidateCode()" alt="" id="validateImg">    </div>    <button type="submit" class="btn btn-default">登录</button></form><script  src="./js/jquery-3.2.1.min.js"></script><script  src="./js/md5.js"></script><script>    function changeValidateCode(){        var Obj = $('#validateImg');        var dt = new Date();        var src = "./php/getValidate.php?t="+dt.getMilliseconds();        Obj.attr('src', src);    }    function chkpwd() {        var obj = $('#exampleInputPassword1');        if(obj.val()!='')  {            var s = md5($('#id').val()+md5(obj.val()).substring(0,30).toUpperCase()+'10467').substring(0,30).toUpperCase();            $('#password').attr("value",s);        } else {            $('#password').attr("value",'');        }    }    function chkyzm() {        var obj = $('#exampleInputValidate');        if(obj.val()!='') {            var s=md5(md5(obj.val().toUpperCase()).substring(0,30).toUpperCase()+'10467').substring(0,30).toUpperCase();            $('#validate').attr("value",s);        } else {            $('#validate').attr("value",'');        }    }</script></body></html>

我们来获取验证码 ,注意的是我是吧Cookie存储到了本地

/**  */php/getValidate.php文件  */<?php//Cookie存储文件$cookie_file = dirname(__FILE__)."/../cookie/tmp.cookie";if(!file_exists($cookie_file)) {    $myfile = fopen($cookie_file, "w");    fclose($myfile);}$t = isset($_GET['t'])?$_GET['t']:0;$verify_code_url = "http://jwgl.xxxxxx.edu.cn/jwweb/sys/ValidateCode.aspx?t=".$t;//不用纠结那条需要不需要,直接header都写上是不会错的$header = [    'Accept:image/webp,image/apng,image/*,*/*;q=0.8',    'Accept-Encoding:gzip, deflate',    'Accept-Language:zh-CN,zh;q=0.8',    'Cache-Control:no-cache',    'Connection:keep-alive',    'Host:jwgl.xxxx.edu.cn',  //修改名称    'Pragma:no-cache',    'Referer:http://jwgl.xxxxx.edu.cn/jwweb/_data/login.aspx',//修改名称    'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36',];$curl = curl_init();curl_setopt($curl,CURLOPT_HTTPHEADER,$header);  //设置表头curl_setopt($curl, CURLOPT_URL, $verify_code_url); // 设置请求地址curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_file); //获取COOKIE并存储$img = curl_exec($curl);curl_close($curl);//输出图片到htmlecho $img;

当进入test.html 时,cookie文件夹下就有存储的Cookie了
这里写图片描述

4. 模拟登陆

然后我们模拟登录,主要的地方是要用之前存储的Cookie和用Post请求

//Cookie路径$cookie_file = dirname(__FILE__)."/../cookie/tmp.cookie";$url = 'http://jwgl.xxxx.edu.cn/jwweb/_data/index_LOGIN.aspx';$post = [    '__VIEWSTATE' => 'dDw4ODEwMTkyNTY7Oz6uXw9RQf0bw8SrGIjZutgOtpxLCw==',    '__VIEWSTATEGENERATOR' =>'4B596BA9',    'pcInfo' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3080.5 Safari/537.36undefined5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3080.5 Safari/537.36 SN:NULL',    'typeName' => '(unable to decode value)',    'dsdsdsdsdxcxdfgfg' => $_POST['password'],    'fgfggfdgtyuuyyuuckjg' => $_POST['validate'],    'Sel_Type' => 'STU',    'txt_asmcdefsddsd' => $_POST['txt_asmcdefsddsd'],    'txt_pewerwedsdfsdff'=> '',    'txt_sdertfgsadscxcadsads' => '',    'sbtState' => '',];$post = http_build_query($post);$headers = array(    "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",    "Accept-Encoding:gzip, deflate",    "Accept-Language:zh-CN,zh;q=0.8",    "Cache-Control:max-age=0",    "Content-Length:603",    "Content-Type:application/x-www-form-urlencoded",    "Host:jwgl.xxxx.edu.cn",    "Origin:http://jwgl.xxxx.edu.cn",    "Proxy-Connection:keep-alive",    "Referer:http://jwgl.xxxx.edu.cn/jwweb/_data/index_LOGIN.aspx",    "Upgrade-Insecure-Requests:1",    "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3080.5 Safari/537.36",);$curl = curl_init();curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//设置headercurl_setopt($curl, CURLOPT_URL, $url);  //设置urlcurl_setopt($curl, CURLOPT_POST, true); // 设置为POST请求curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。curl_setopt($curl, CURLOPT_POSTFIELDS, $post); //传送的数据curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);  //设置cookie$result=curl_exec($curl);//可以输出当前信息看看是否登录成功//$file = dirname(__FILE__)."/../html/test.html";//$fp = fopen($file,"w");//fwrite($fp,$result);//fwrite($fp, '结束');//fclose($fp);

5. 获取数据

到此,我们已经登录成功了,然后我们就可以进入系统提取数据了,比如提取课表信息。
这里写图片描述

$curl = curl_init();$url = 'http://jwgl.xxx.edu.cn/jwweb/wsxk/stu_zxjg_rpt.aspx';curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_HEADER, false);curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);$result=curl_exec($curl);$file = dirname(__FILE__)."/../html/test.html";$fp = fopen($file,"w");fwrite($fp,$result);fclose($fp);

6. 提取数据

当我们得到网页文本时,并不是我们的最终目的,我们要的是其中除了html标签之外的数据。关于提取数据,我推荐大家使用symfony/dom-crawler,再配合他的symfony/css-selector来将html文本转换成结点,通过CSS选择器方式定位结点获取相应的数据。

阅读全文
2 0
原创粉丝点击