123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- namespace app\modules\admin\controllers;
- use app\common\controllers\BController;
- use Yii;
- class KaoyanController extends BController
- {
- public $layout = 'main';
- //数据采集
- public function actionData()
- {
- $do = Yii::$app->request->get('do');
- //采集学校数据
- if($do=='school')
- {
- $sql = "select * from {{%collect_content}} where status=1 order by id asc limit 1";
- $schoolResult = Yii::$app->db->createCommand($sql)->queryOne();
- if(empty($schoolResult)){exit("导入院校完成");}
- $schoolInfo = string2array($schoolResult['data']);
- $sql = "insert into {{%ky_school}} set region_name='".$schoolInfo['region']."',title='".$schoolInfo['title']."',special='".$schoolInfo['special']."',logo='".addslashes($schoolInfo['logo'])."',owner='".$schoolInfo['owner']."'";
- Yii::$app->db->createCommand($sql)->execute();
- $sql = "update {{%collect_content}} set status=2 where id = ".$schoolResult['id'];
- Yii::$app->db->createCommand($sql)->execute();
- echo "<script>";
- echo "window.location.reload();";
- echo "</script>";
- exit;
- }
- //采集专业数据
- if($do=='major')
- {
- $sql = "select * from {{%ky_major}} where status=0 and parent_id=0 order by id asc limit 1";
- $parentMajor = Yii::$app->db->createCommand($sql)->queryOne();
- if(empty($parentMajor)){exit("采集专业完成");}
- $parentCode = $parentMajor['type'].$parentMajor['code'];
- $data = ['method'=>'subCategoryMl','key'=>$parentCode];
- $result = https_request('https://yz.chsi.com.cn/zyk/specialityCategory.do',[],$data);
- preg_match_all("/<li[^>]*id='(.*?)'[^>]*>(.*)<\/li>/i",$result,$matches);
- foreach($matches[1] as $k=>$v)
- {
- $code = substr($v,2);
- $title = str_replace('','',strip_tags($matches[2][$k]));
- $sql = "insert into {{%ky_major}} set status=0,parent_id=".$parentMajor['id'].",code='".$code."',type='".$parentMajor['type']."',title='".$title."'";
- Yii::$app->db->createCommand($sql)->execute();
- }
- $sql = "update {{%ky_major}} set status=1 where id = ".$parentMajor['id'];
- Yii::$app->db->createCommand($sql)->execute();
- echo "<script>";
- echo "window.location.reload();";
- echo "</script>";
- exit;
- }
- //采集专业数据
- if($do=='childmajor')
- {
- $sql = "select * from {{%ky_major}} where status=0 order by id asc limit 1";
- $parentMajor = Yii::$app->db->createCommand($sql)->queryOne();
- if(empty($parentMajor)){exit("采集专业完成");}
- $parentCode = $parentMajor['type'].$parentMajor['code'];
- $data = ['method'=>'subCategoryXk','key'=>$parentCode];
- $result = https_request('https://yz.chsi.com.cn/zyk/specialityCategory.do',[],$data);
- $result = $this->compressHtml($result);
- preg_match_all('/<tr.*?>(.*?)<\/tr>/im',$result,$matches);
- foreach($matches[1] as $k=>$v)
- {
- if($k==0)continue;
- preg_match_all('/<td.*?>(.*?)<\/td>/im',$v,$matchesChild);
- $code = $matchesChild[1][1];
- $title = str_replace('','',strip_tags($matchesChild[1][0]));
- $sql = "insert into {{%ky_major}} set status=1,parent_id=".$parentMajor['id'].",code='".$code."',type='".$parentMajor['type']."',title='".$title."'";
- Yii::$app->db->createCommand($sql)->execute();
- }
- $sql = "update {{%ky_major}} set status=1 where id = ".$parentMajor['id'];
- Yii::$app->db->createCommand($sql)->execute();
- echo "<script>";
- echo "window.location.reload();";
- echo "</script>";
- exit;
- }
- //采集院校专业数据
- if($do=='schoolmajor')
- {
- $sql = "select * from {{%ky_major}} where status=0 order by id asc limit 1";
- $major = Yii::$app->db->createCommand($sql)->queryOne();
- if(empty($major)){exit("采集完成");}
- $url = "https://yz.chsi.com.cn/zyk/specialityDetail.do?zymc=".urlencode($major['title'])."&zydm=".$major['code']."&cckey=".$major['type']."&ssdm=&method=distribution";
- $result = $this->compressHtml(https_request($url));
- if(strpos($result,'很抱歉')===false)
- {
- $result = $this->cut_html($result,'<div class="tab-container zyk-zyfb-tab">','<div class="zyk-right">');
- preg_match_all("/<li[^>]*>(.*?)<\/li>/i",$result,$matches);
- if(!empty($matches[1]))foreach($matches[1] as $schoolTit)
- {
- $schoolName = strip_tags($schoolTit);
- $sql = "insert into {{%ky_school_major}} set school_title='".$schoolName."',major_title='".$major['title']."',major_code='".$major['code']."'";
- Yii::$app->db->createCommand($sql)->execute();
- }
- }
- $sql = "update {{%ky_major}} set status=1 where id = ".$major['id'];
- Yii::$app->db->createCommand($sql)->execute();
- echo "<script>";
- echo "window.location.reload();";
- echo "</script>";
- exit;
- }
- //采集专业目录
- if($do=='majordir')
- {
- $sql = "select * from {{%ky_school_major}} where status=0 order by id asc limit 1";
- $unit = Yii::$app->db->createCommand($sql)->queryOne();
- if(empty($unit)){exit("采集完成");}
- $url = "https://yz.chsi.com.cn/zsml/querySchAction.do?dwmc=".urlencode($unit['school_title'])."&mlmc=&yjxkdm=".$unit['major_code'];
- $result = $this->compressHtml(https_request($url));
- $result = $this->cut_html($result,'<table class="ch-table more-content">','<div class="zsml-page-box">');
- if(strpos($result,'很抱歉')===false)
- {
- preg_match_all('/<a(.*?)href=[\'"](.*?)[\'"](.*?)>(.*?)<\/a>/i',$result,$matches);
- if(!empty($matches[4]))foreach($matches[4] as $k=>$linkTit)
- {
- if($linkTit=='查看')
- {
- $url = 'https://yz.chsi.com.cn'.$matches[2][$k];
- $sql = "insert into {{%ky_major_dir}} set school_title='".$unit['school_title']."',major_code='".$unit['major_code']."',url='".$url."'";
- Yii::$app->db->createCommand($sql)->execute();
- }
- }
- }
- $sql = "update {{%ky_school_major}} set status=1 where id = ".$unit['id'];
- Yii::$app->db->createCommand($sql)->execute();
- echo "<script>";
- echo "window.location.reload();";
- echo "</script>";
- exit;
- }
- //采集专业目录详情
- if($do=='dirdetail')
- {
- $sql = "select * from {{%ky_major_dir}} where status=0 order by id asc limit 1";
- $unit = Yii::$app->db->createCommand($sql)->queryOne();
- if(empty($unit)){exit("采集完成");}
- $url = $unit['url'];
- $result = $this->compressHtml(https_request($url));
- //$result = $this->cut_html($result,'<table class="ch-table more-content">','<div class="zsml-page-box">');
- $exam_way = $this->cut_html($result,'<td class="zsml-title">考试方式:</td><td class="zsml-summary">','</td>');
- $direction = $this->cut_html($result,'<td class="zsml-title">研究方向:</td><td class="zsml-summary">','</td>');
- $learn_way = $this->cut_html($result,'<td class="zsml-title">学习方式:</td><td class="zsml-summary">','</td>');
- $student_num = $this->cut_html($result,'<td class="zsml-title">拟招人数:</td><td class="zsml-summary">','</td>');
- $exam_content = $this->cut_html($result,'<div class="zsml-result">','</div><!-- 考试范围 end -->');
- $exam_content = str_replace('<span class="sub-msg">见招生简章</span>','',$exam_content);
- $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'];
- Yii::$app->db->createCommand($sql)->execute();
- echo "<script>";
- echo "window.location.reload();";
- echo "</script>";
- exit;
- }
- //采集科目
- if($do=='course')
- {
- $sql = "select * from {{%ky_major_dir}} where status=0 order by id asc limit 1";
- $unit = Yii::$app->db->createCommand($sql)->queryOne();
- if(empty($unit)){exit("采集完成");}
- $result = $unit['exam_content'];
- $result = str_replace('<td colspan="4"><div><span>或</span><hr class="zsml-line"></div></td>','',$result);
- $result = preg_replace('/<span[^>]*class="sub-msg"[^>]*>(.*?)<\/span>/i','',$result);
- preg_match_all('/<td.*?>(.*?)<\/td>/im',$result,$matches);
- if($matches[1])foreach($matches[1] as $course)
- {
- $short_name = str_replace(['(',')'],'',$course);
- $short_name = explode("(",$short_name);
- $short_name = $short_name[0];
- $sql = "select * from {{%ky_course}} where short_name='".$short_name."'";
- $row = Yii::$app->db->createCommand($sql)->queryOne();
- if(empty($row))
- {
- $sql = "insert into {{%ky_course}} set course='".$course."',short_name='".$short_name."'";
- Yii::$app->db->createCommand($sql)->execute();
- }
- }
- $sql = "update {{%ky_major_dir}} set status=1 where id = ".$unit['id'];
- Yii::$app->db->createCommand($sql)->execute();
- echo "<script>";
- echo "window.location.reload();";
- echo "</script>";
- exit;
- }
- }
- //压缩html
- public function compressHtml($str){
- $str = trim($str);
- $str = str_replace("\t","",$str);
- $str = str_replace("\r\n","",$str);
- $str = str_replace("\r","",$str);
- $str = str_replace("\n","",$str);
- $str=preg_replace("/>[ ]+/si",">",$str); //过滤>(">"号后面带空格)
- $str=preg_replace("/[ ]+</si","<",$str); //过滤<("<"号前面带空格)
- return trim($str);
- }
- public function cut_html($html, $start, $end) {
- //if(strpos($html,$start)===false) return false;
- if (empty($html)||empty($start)||empty($end)) return false;
- $html = str_replace(array("\r", "\n"), "", $html);
- $start = str_replace(array("\r", "\n"), "", $start);
- $end = str_replace(array("\r", "\n"), "", $end);
- $html = explode(trim($start), $html);
- if(is_array($html)&&isset($html[1])) $html = explode(trim($end), $html[1]);
- return $html[0];
- }
- }
|