KaoyanController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace app\modules\admin\controllers;
  3. use app\common\controllers\BController;
  4. use Yii;
  5. class KaoyanController extends BController
  6. {
  7. public $layout = 'main';
  8. //数据采集
  9. public function actionData()
  10. {
  11. $do = Yii::$app->request->get('do');
  12. //采集学校数据
  13. if($do=='school')
  14. {
  15. $sql = "select * from {{%collect_content}} where status=1 order by id asc limit 1";
  16. $schoolResult = Yii::$app->db->createCommand($sql)->queryOne();
  17. if(empty($schoolResult)){exit("导入院校完成");}
  18. $schoolInfo = string2array($schoolResult['data']);
  19. $sql = "insert into {{%ky_school}} set region_name='".$schoolInfo['region']."',title='".$schoolInfo['title']."',special='".$schoolInfo['special']."',logo='".addslashes($schoolInfo['logo'])."',owner='".$schoolInfo['owner']."'";
  20. Yii::$app->db->createCommand($sql)->execute();
  21. $sql = "update {{%collect_content}} set status=2 where id = ".$schoolResult['id'];
  22. Yii::$app->db->createCommand($sql)->execute();
  23. echo "<script>";
  24. echo "window.location.reload();";
  25. echo "</script>";
  26. exit;
  27. }
  28. //采集专业数据
  29. if($do=='major')
  30. {
  31. $sql = "select * from {{%ky_major}} where status=0 and parent_id=0 order by id asc limit 1";
  32. $parentMajor = Yii::$app->db->createCommand($sql)->queryOne();
  33. if(empty($parentMajor)){exit("采集专业完成");}
  34. $parentCode = $parentMajor['type'].$parentMajor['code'];
  35. $data = ['method'=>'subCategoryMl','key'=>$parentCode];
  36. $result = https_request('https://yz.chsi.com.cn/zyk/specialityCategory.do',[],$data);
  37. preg_match_all("/<li[^>]*id='(.*?)'[^>]*>(.*)<\/li>/i",$result,$matches);
  38. foreach($matches[1] as $k=>$v)
  39. {
  40. $code = substr($v,2);
  41. $title = str_replace('&#xe6a2;','',strip_tags($matches[2][$k]));
  42. $sql = "insert into {{%ky_major}} set status=0,parent_id=".$parentMajor['id'].",code='".$code."',type='".$parentMajor['type']."',title='".$title."'";
  43. Yii::$app->db->createCommand($sql)->execute();
  44. }
  45. $sql = "update {{%ky_major}} set status=1 where id = ".$parentMajor['id'];
  46. Yii::$app->db->createCommand($sql)->execute();
  47. echo "<script>";
  48. echo "window.location.reload();";
  49. echo "</script>";
  50. exit;
  51. }
  52. //采集专业数据
  53. if($do=='childmajor')
  54. {
  55. $sql = "select * from {{%ky_major}} where status=0 order by id asc limit 1";
  56. $parentMajor = Yii::$app->db->createCommand($sql)->queryOne();
  57. if(empty($parentMajor)){exit("采集专业完成");}
  58. $parentCode = $parentMajor['type'].$parentMajor['code'];
  59. $data = ['method'=>'subCategoryXk','key'=>$parentCode];
  60. $result = https_request('https://yz.chsi.com.cn/zyk/specialityCategory.do',[],$data);
  61. $result = $this->compressHtml($result);
  62. preg_match_all('/<tr.*?>(.*?)<\/tr>/im',$result,$matches);
  63. foreach($matches[1] as $k=>$v)
  64. {
  65. if($k==0)continue;
  66. preg_match_all('/<td.*?>(.*?)<\/td>/im',$v,$matchesChild);
  67. $code = $matchesChild[1][1];
  68. $title = str_replace('&#xe6a2;','',strip_tags($matchesChild[1][0]));
  69. $sql = "insert into {{%ky_major}} set status=1,parent_id=".$parentMajor['id'].",code='".$code."',type='".$parentMajor['type']."',title='".$title."'";
  70. Yii::$app->db->createCommand($sql)->execute();
  71. }
  72. $sql = "update {{%ky_major}} set status=1 where id = ".$parentMajor['id'];
  73. Yii::$app->db->createCommand($sql)->execute();
  74. echo "<script>";
  75. echo "window.location.reload();";
  76. echo "</script>";
  77. exit;
  78. }
  79. //采集院校专业数据
  80. if($do=='schoolmajor')
  81. {
  82. $sql = "select * from {{%ky_major}} where status=0 order by id asc limit 1";
  83. $major = Yii::$app->db->createCommand($sql)->queryOne();
  84. if(empty($major)){exit("采集完成");}
  85. $url = "https://yz.chsi.com.cn/zyk/specialityDetail.do?zymc=".urlencode($major['title'])."&zydm=".$major['code']."&cckey=".$major['type']."&ssdm=&method=distribution";
  86. $result = $this->compressHtml(https_request($url));
  87. if(strpos($result,'很抱歉')===false)
  88. {
  89. $result = $this->cut_html($result,'<div class="tab-container zyk-zyfb-tab">','<div class="zyk-right">');
  90. preg_match_all("/<li[^>]*>(.*?)<\/li>/i",$result,$matches);
  91. if(!empty($matches[1]))foreach($matches[1] as $schoolTit)
  92. {
  93. $schoolName = strip_tags($schoolTit);
  94. $sql = "insert into {{%ky_school_major}} set school_title='".$schoolName."',major_title='".$major['title']."',major_code='".$major['code']."'";
  95. Yii::$app->db->createCommand($sql)->execute();
  96. }
  97. }
  98. $sql = "update {{%ky_major}} set status=1 where id = ".$major['id'];
  99. Yii::$app->db->createCommand($sql)->execute();
  100. echo "<script>";
  101. echo "window.location.reload();";
  102. echo "</script>";
  103. exit;
  104. }
  105. //采集专业目录
  106. if($do=='majordir')
  107. {
  108. $sql = "select * from {{%ky_school_major}} where status=0 order by id asc limit 1";
  109. $unit = Yii::$app->db->createCommand($sql)->queryOne();
  110. if(empty($unit)){exit("采集完成");}
  111. $url = "https://yz.chsi.com.cn/zsml/querySchAction.do?dwmc=".urlencode($unit['school_title'])."&mlmc=&yjxkdm=".$unit['major_code'];
  112. $result = $this->compressHtml(https_request($url));
  113. $result = $this->cut_html($result,'<table class="ch-table more-content">','<div class="zsml-page-box">');
  114. if(strpos($result,'很抱歉')===false)
  115. {
  116. preg_match_all('/<a(.*?)href=[\'"](.*?)[\'"](.*?)>(.*?)<\/a>/i',$result,$matches);
  117. if(!empty($matches[4]))foreach($matches[4] as $k=>$linkTit)
  118. {
  119. if($linkTit=='查看')
  120. {
  121. $url = 'https://yz.chsi.com.cn'.$matches[2][$k];
  122. $sql = "insert into {{%ky_major_dir}} set school_title='".$unit['school_title']."',major_code='".$unit['major_code']."',url='".$url."'";
  123. Yii::$app->db->createCommand($sql)->execute();
  124. }
  125. }
  126. }
  127. $sql = "update {{%ky_school_major}} set status=1 where id = ".$unit['id'];
  128. Yii::$app->db->createCommand($sql)->execute();
  129. echo "<script>";
  130. echo "window.location.reload();";
  131. echo "</script>";
  132. exit;
  133. }
  134. //采集专业目录详情
  135. if($do=='dirdetail')
  136. {
  137. $sql = "select * from {{%ky_major_dir}} where status=0 order by id asc limit 1";
  138. $unit = Yii::$app->db->createCommand($sql)->queryOne();
  139. if(empty($unit)){exit("采集完成");}
  140. $url = $unit['url'];
  141. $result = $this->compressHtml(https_request($url));
  142. //$result = $this->cut_html($result,'<table class="ch-table more-content">','<div class="zsml-page-box">');
  143. $exam_way = $this->cut_html($result,'<td class="zsml-title">考试方式:</td><td class="zsml-summary">','</td>');
  144. $direction = $this->cut_html($result,'<td class="zsml-title">研究方向:</td><td class="zsml-summary">','</td>');
  145. $learn_way = $this->cut_html($result,'<td class="zsml-title">学习方式:</td><td class="zsml-summary">','</td>');
  146. $student_num = $this->cut_html($result,'<td class="zsml-title">拟招人数:</td><td class="zsml-summary">','</td>');
  147. $exam_content = $this->cut_html($result,'<div class="zsml-result">','</div><!-- 考试范围 end -->');
  148. $exam_content = str_replace('<span class="sub-msg">见招生简章</span>','',$exam_content);
  149. $sql = "update {{%ky_major_dir}} set status=1,exam_way='".$exam_way."',direction='".addslashes($direction)."',learn_way='".$learn_way."',student_num='".$student_num."',exam_content='".addslashes($exam_content)."' where id = ".$unit['id'];
  150. Yii::$app->db->createCommand($sql)->execute();
  151. echo "<script>";
  152. echo "window.location.reload();";
  153. echo "</script>";
  154. exit;
  155. }
  156. //采集科目
  157. if($do=='course')
  158. {
  159. $sql = "select * from {{%ky_major_dir}} where status=0 order by id asc limit 1";
  160. $unit = Yii::$app->db->createCommand($sql)->queryOne();
  161. if(empty($unit)){exit("采集完成");}
  162. $result = $unit['exam_content'];
  163. $result = str_replace('<td colspan="4"><div><span>或</span><hr class="zsml-line"></div></td>','',$result);
  164. $result = preg_replace('/<span[^>]*class="sub-msg"[^>]*>(.*?)<\/span>/i','',$result);
  165. preg_match_all('/<td.*?>(.*?)<\/td>/im',$result,$matches);
  166. if($matches[1])foreach($matches[1] as $course)
  167. {
  168. $short_name = str_replace(['(',')'],'',$course);
  169. $short_name = explode("(",$short_name);
  170. $short_name = $short_name[0];
  171. $sql = "select * from {{%ky_course}} where short_name='".$short_name."'";
  172. $row = Yii::$app->db->createCommand($sql)->queryOne();
  173. if(empty($row))
  174. {
  175. $sql = "insert into {{%ky_course}} set course='".$course."',short_name='".$short_name."'";
  176. Yii::$app->db->createCommand($sql)->execute();
  177. }
  178. }
  179. $sql = "update {{%ky_major_dir}} set status=1 where id = ".$unit['id'];
  180. Yii::$app->db->createCommand($sql)->execute();
  181. echo "<script>";
  182. echo "window.location.reload();";
  183. echo "</script>";
  184. exit;
  185. }
  186. }
  187. //压缩html
  188. public function compressHtml($str){
  189. $str = trim($str);
  190. $str = str_replace("\t","",$str);
  191. $str = str_replace("\r\n","",$str);
  192. $str = str_replace("\r","",$str);
  193. $str = str_replace("\n","",$str);
  194. $str=preg_replace("/>[ ]+/si",">",$str); //过滤>(">"号后面带空格)
  195. $str=preg_replace("/[ ]+</si","<",$str); //过滤<("<"号前面带空格)
  196. return trim($str);
  197. }
  198. public function cut_html($html, $start, $end) {
  199. //if(strpos($html,$start)===false) return false;
  200. if (empty($html)||empty($start)||empty($end)) return false;
  201. $html = str_replace(array("\r", "\n"), "", $html);
  202. $start = str_replace(array("\r", "\n"), "", $start);
  203. $end = str_replace(array("\r", "\n"), "", $end);
  204. $html = explode(trim($start), $html);
  205. if(is_array($html)&&isset($html[1])) $html = explode(trim($end), $html[1]);
  206. return $html[0];
  207. }
  208. }