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 ""; 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("/]*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 ""; 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>/im',$result,$matches); foreach($matches[1] as $k=>$v) { if($k==0)continue; preg_match_all('/(.*?)<\/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 ""; 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,'
','
'); preg_match_all("/]*>(.*?)<\/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 ""; 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,'','
'); if(strpos($result,'很抱歉')===false) { preg_match_all('/(.*?)<\/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 ""; 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,'
','
'); $exam_way = $this->cut_html($result,'
'); $direction = $this->cut_html($result,''); $learn_way = $this->cut_html($result,''); $student_num = $this->cut_html($result,''); $exam_content = $this->cut_html($result,'
','
'); $exam_content = str_replace('见招生简章','',$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 ""; 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('','',$result); $result = preg_replace('/]*class="sub-msg"[^>]*>(.*?)<\/span>/i','',$result); preg_match_all('/(.*?)<\/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 ""; 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("/[ ]+
考试方式:','研究方向:','学习方式:','拟招人数:','