国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php實現模擬登陸方正教務系統抓取課表

2024-05-04 23:35:24
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php實現模擬登陸方正教務系統抓取課表的相關資料,需要的朋友可以參考下

課程格子和超級課程表這兩個應用,想必大學生都很熟悉,使用自己的學號和教務系統的密碼,就可以將自己的課表導入,隨時隨地都可以在手機上查看。

其實稍微了解一點php的話,我們也可以做一個類似這樣的web 應用。

1,解決掉驗證碼

其實這是正方的一個小bug,當我們進入登陸界面時,瀏覽器會去請求服務器,服務器會生成一個驗證碼圖片。如果我們不去請求這個圖片,那么正方后臺也不會生成相應的 驗證碼,于是這樣我們就有了可乘之機,讓我高興會兒~這時,我們在不填寫驗證碼的情況下,可以很流暢的進入。大家可以在自己的電腦上禁止訪問驗證碼的地址,然后試試這 是不是真的~當然,這只對正方有效。

2,php 的curl 模擬登陸

接下來就是相關代碼了,相信很多人和我一樣,只喜歡看例子,對于長篇大論的講解,轉頭就走……不過這個習慣還是不好……廢話不多說!

 

 
  1. //模擬登陸 
  2. function curl_request($url,$post='',$cookie=''$returnCookie=0){ 
  3. $curl = curl_init(); 
  4. curl_setopt($curl, CURLOPT_URL, $url); 
  5. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)'); 
  6. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 
  7. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); 
  8. curl_setopt($curl, CURLOPT_REFERER, "這里一定要換成教務系統登陸的url"); //填寫教務系統url 
  9. if($post) { 
  10. curl_setopt($curl, CURLOPT_POST, 1); 
  11. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post)); 
  12. if($cookie) { 
  13. curl_setopt($curl, CURLOPT_COOKIE, $cookie); 
  14. curl_setopt($curl, CURLOPT_HEADER, $returnCookie); 
  15. curl_setopt($curl, CURLOPT_TIMEOUT, 20); 
  16. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  17. $data = curl_exec($curl); 
  18. if (curl_errno($curl)) { 
  19. return curl_error($curl); 
  20. curl_close($curl); 
  21. if($returnCookie){ 
  22. list($header$body) = explode("/r/n/r/n"$data, 2); 
  23. preg_match_all("/Set/-Cookie:([^;]*);/"$header$matches); 
  24. $info['cookie'] = substr($matches[1][0], 1); 
  25. $info['content'] = $body
  26. return $info
  27. }else
  28. return $data

3,教務系統登陸頁面的隱藏字段

舉個栗子

復制代碼代碼如下:

 

  1. <input type="hidden" name="__VIEWSTATE" value="dDwyODE2NTM0OTg7Oz61eIbnKVojBioGYtg2vsy2SklwiA=="

這些東西在登陸的時候也是需要帶上的,順便貼出函數,順便暴漏了博主的學校……皇家種地大學(主要是正則表達式的運用)

 

 
  1. //登陸頁面的隱藏字段   
  2.  
  3. function getView(){ 
  4. $url = 'http://jw.hzau.edu.cn/default2.aspx'
  5. $result = curl_request($url); 
  6. $pattern = '/<input type="hidden" name="__VIEWSTATE" value="(.*?)" //>/is'
  7. preg_match_all($pattern$result$matches); 
  8. $res[0] = $matches[1][0]; 
  9.  
  10. return $res[0] ; 
  11.   //返回教室查詢頁面的隱藏值 
  12.   private function getViewJs($cookie,$xh){ 
  13.     $url = "http://jw.hzau.edu.cn/xxjsjy.aspx?xh={$xh}"
  14.     $result = curl_request($url,'',$cookie); 
  15.     $pattern = '/<input type="hidden" name="__VIEWSTATE" value="(.*?)" //>/is'
  16.     preg_match_all($pattern$result$matches); 
  17.     $res[0] = $matches[1][0]; 
  18.     return $res[0] ; 
  19.   } 

4,cookie 的獲取

 

 
  1. function login($xh,$pwd){ 
  2. $url = 'http://jw.hzau.edu.cn/default2.aspx'
  3. $post['__VIEWSTATE'] = $this->getView(); 
  4. $post['txtUserName'] = $xh//填寫學號 
  5. $post['TextBox2'] = $pwd//填寫密碼 
  6. $post['txtSecretCode'] = ''
  7. $post['lbLanguage'] = ''
  8. $post['hidPdrs'] = ''
  9. $post['hidsc'] = ''
  10. $post['RadioButtonList1'] = iconv('utf-8''gb2312''學生'); 
  11. $post['Button1'] = iconv('utf-8''gb2312''登錄'); 
  12. $result = curl_request($url,$post,'', 1); 
  13. return $result['cookie']; 

5,讓我們來試試查課表的功能,格式有點亂額,大家湊合著看,我把課表轉成了一個二維關聯數組

 

 
  1. //返回課表字符串 
  2. private function classresult($xh,$pwd){ 
  3. date_default_timezone_set("PRC"); //時區設置 
  4. $classList = "";//聲明課表變量 
  5.  
  6. $cookie = $this->login($xh,$pwd); 
  7. $view = $this->getViewJs($cookie,$xh);//驗證密碼是否正確 
  8.  
  9. //如果密碼正確 
  10. if (!emptyempty($view)) { 
  11. $url = "http://jw.hzau.edu.cn/xskbcx.aspx?xh={$xh}"
  12. $result = curl_request($url,'',$cookie); //保存的cookies 
  13. preg_match_all('/<table id="Table1"[/w/W]*?>([/w/W]*?)<//table>/',$result,$out); 
  14. $table = $out[0][0]; //獲取整個課表 
  15.  
  16. preg_match_all('/<td [/w/W]*?>([/w/W]*?)<//td>/',$table,$out); 
  17. $td = $out[1]; 
  18. $length = count($td); 
  19.  
  20. //獲得課程列表 
  21. for ($i=0; $i < $length$i++) { 
  22. $td[$i] = str_replace("<br>"""$td[$i]); 
  23.  
  24. $reg = "/{(.*)}/"
  25.  
  26. if (!preg_match_all($reg$td[$i], $matches)) { 
  27. unset($td[$i]); 
  28.  
  29. $td = array_values($td); //將課程列表數組重新索引 
  30. $tdLength = count($td); 
  31. for ($i=0; $i < $tdLength$i++) { 
  32. $td[$i] = iconv('GB2312','UTF-8',$td[$i]); 
  33.  
  34. //將課表轉換成數組形式 
  35. function converttoTable($table){ 
  36. $list = array
  37. 'sun' => array
  38. '1,2' => ''
  39. '3,4' => ''
  40. '5,6' => ''
  41. '7,8' => ''
  42. '9,10' => '' 
  43. ), 
  44. 'mon' => array
  45. '1,2' => ''
  46. '3,4' => ''
  47. '5,6' => ''
  48. '7,8' => ''
  49. '9,10' => '' 
  50. ), 
  51. 'tues' => array
  52. '1,2' => ''
  53. '3,4' => ''
  54. '5,6' => ''
  55. '7,8' => ''
  56. '9,10' => '' 
  57. ), 
  58. 'wed' => array
  59. '1,2' => ''
  60. '3,4' => ''
  61. '5,6' => ''
  62. '7,8' => ''
  63. '9,10' => '' 
  64. ), 
  65. 'thur' => array
  66. '1,2' => ''
  67. '3,4' => ''
  68. '5,6' => ''
  69. '7,8' => ''
  70. '9,10' => '' 
  71. ), 
  72. 'fri' => array
  73. '1,2' => ''
  74. '3,4' => ''
  75. '5,6' => ''
  76. '7,8' => ''
  77. '9,10' => '' 
  78. ), 
  79. 'sat' => array
  80. '1,2' => ''
  81. '3,4' => ''
  82. '5,6' => ''
  83. '7,8' => ''
  84. '9,10' => '' 
  85. ); 
  86. $week = array("sun"=>"周日","mon"=>"周一","tues"=>"周二","wed"=>"周三","thur"=>"周四","fri"=>"周五","sat"=>"周六"); 
  87. $order = array('1,2','3,4','5,6','7,8','9,10'); 
  88. foreach ($table as $key => $value) { 
  89. $class = $value
  90. foreach ($week as $key => $weekDay) { 
  91. $pos = strpos($class,$weekDay); 
  92. // echo $pos; 
  93. if ($pos) { 
  94. $weekArrayDay = $key//獲取list數組中的第一維key 
  95. foreach ($order as $key => $orderClass) { 
  96. $pos = strpos($class,$orderClass); 
  97. if ($pos) { 
  98. $weekArrayOrder = $orderClass//獲取該課程是第幾節 
  99. break
  100. break
  101. $list[$weekArrayDay][$weekArrayOrder] = $class
  102. return $list
  103.  
  104. //調用函數 
  105. return converttoTable($td); 
  106. }else
  107. return 0; 

6,再試試查詢空教室的功能

 

 
  1. //空教室查詢結果 
  2. public function roomresult(){ 
  3. $xh = ""//設置學號 
  4. $pwd = ""//學號對應的密碼 
  5.  
  6. $cookie = $this->login($xh,$pwd); 
  7. $url = "http://jw.hzau.edu.cn/xs_main.aspx?xh={$xh}"
  8. $result = curl_request($url,'',$cookie); //保存的cookies 
  9.  
  10. $url="http://jw.hzau.edu.cn/xxjsjy.aspx?xh={$xh}"
  11. $post['Button2'] = iconv('utf-8''gb2312''空教室查詢'); 
  12. $post['__EVENTARGUMENT']=''
  13. $post['__EVENTTARGET']=''
  14. $post['__VIEWSTATE'] = $this->getViewJs($cookie,$xh); 
  15. $post['ddlDsz'] = iconv('utf-8''gb2312''單'); 
  16. $post['ddlSyXn'] = '2014-2015'//學年 
  17. $post['ddlSyxq'] = '1'
  18. $post['jslb'] = ''
  19. $post['xiaoq'] = ''
  20.  
  21. $post['kssj']=$_GET['start']; //提交的開始查詢時間  
  22. $post['sjd']=$_GET['class'];//提交的課程節次 
  23.  
  24. $post['xn']='2014-2015';//所在學年 
  25. $post['xq']='2';//所在學期 
  26. $post['xqj']='6';//當天星期幾 
  27. $post['dpDataGrid1:txtPageSize']=90;//每頁顯示條數 
  28.  
  29. $result = curl_request($url,$post,$cookie,0); 
  30.  
  31. preg_match_all('/<span[^>]+>[^>]+span>/',$result,$out); 
  32. $tip = iconv('gb2312''utf-8'$out[0][3]);//獲取頁面前部的提示內容 
  33. preg_match_all('/<table[/w/W]*?>([/w/W]*?)<//table>/',$result,$out); 
  34. $table = iconv('gb2312''utf-8'$out[0][0]); //獲取查詢列表 
  35.  
  36. $this->load->view("classroom",array('tip'=>$tip,'table'=>$table)); 

總結起來就是這些了,每個學校的教務系統都不盡相同,這時我們可以借助火狐瀏覽器的 firebug 抓包,看看到底提交了哪些東西。

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 齐齐哈尔市| 页游| 澄江县| 治多县| 六枝特区| 沂南县| 黄石市| 仁怀市| 玉门市| 枣庄市| 赤城县| 吉木乃县| 岑巩县| 尼玛县| 大连市| 东兰县| 乾安县| 留坝县| 榆树市| 方城县| 富平县| 鄯善县| 进贤县| 昌宁县| 东安县| 四川省| 简阳市| 石家庄市| 金山区| 阜平县| 高尔夫| 武汉市| 英超| 台北县| 浦东新区| 垦利县| 鄂托克旗| 浦东新区| 建湖县| 兴和县| 荣成市|