//将区划数据写入数据库
public function grap(){
//抓取省数据
$matches = $this->exectu("http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2017/index.html","/<a href='([^']\S+)'>([^>]\S+)<br\/><\/a>/");
foreach($matches[1] as $key => $value){
$param = ['name'=>$matches[2][$key],'parent_id'=>0];
$insertId = Db::name('region')->insertGetId($param);
//抓取市数据
$matches_second = $this->exectu("http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2017/".$value,"/<a href='([^']\S+)'>([^>][^\d]+)<\/a>/");
foreach($matches_second[1] as $key_second =>$value_second){
$param_second = ['name'=>$matches_second[2][$key_second],'parent_id'=>$insertId];
$insertId_second = Db::name('region')->insertGetId($param_second);
//抓取区数据
$matches_third = $this->exectu("http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2017/".$value_second,"/<a href='([^']\S+)'>([^>][^\d]+)<\/a>/");
foreach($matches_third[1] as $key_third => $value_third){
$param_third = ['name'=>$matches_third[2][$key_third],'parent_id'=>$insertId_second];
$insertId_third = Db::name('region')->insertGetId($param_third);
}
}
}
}
//curl 抓取区划数据
public function exectu($url,$pattern){
header("Content-Type:text/html; charset=GBK");
set_time_limit(0);
//初始化一个 cURL 对象
$curl = curl_init();
//设置你需要抓取的URL
curl_setopt($curl, CURLOPT_URL, $url);
$user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
//设置header
curl_setopt($curl, CURLOPT_HEADER, 0);
//设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//运行cURL,请求网页
$data = curl_exec($curl);
$data = mb_convert_encoding($data, "utf-8", "GBK");
curl_close($curl);
$isMatched = preg_match_all($pattern, $data, $matches);
return $matches;
}抓取数据并写入数据 用以上2 个方法即可 ,如果需要bootstrap 三级联动前台渲染的朋友 加我qq 769387485
最佳答案