网站上有很多jq的联动,主要是城市地区数据,那我们怎么获取到这些数据并且保存在我们的数据表中,这是国家的数据,下面我们用PHP程序将他们采集下来
1.数据采集
publicfunctioncountryaddress(){
$ModelObj=D('Areas');
$baseUrl='http://www.300.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2023/';
//第一级
$firstDetail=file_get_contents($baseUrl.'index.html');
$firstPattern='/tdahref=\'(\d+).html\'(.*?)br\/\/a\/td/i';
preg_match_all($firstPattern,$firstDetail,$firstResult);
$urllist=$firstResult[1];
$namelist=$firstResult[2];
foreach($namelistas$k=$v){
$iscun=$ModelObj-where(array('name'=iconv('GB2312','UTF-8',$v)))-find();
if(!$iscun){
$adddata=array(
'code'=$k+1,//省级编码暂时默认以排序号来定
'name'=iconv('GB2312','UTF-8',$v),
'parent_id'=0,
);
$first_re=$ModelObj-data($adddata)-add();
if($first_re){
//第二级
$secondDetail=file_get_contents($baseUrl.$urllist[$k].'.html');
$secondPattern=/tdahref=\'.$urllist[$k].\/(\d+?)\.html\'(\d+?)\/a\/tdtdahref=\'.$urllist[$k].\/(\d+?)\.html\'(.*?)\/a/i;
preg_match_all($secondPattern,$secondDetail,$secondResult);
$secondUrlList=$secondResult[1];
$secondCodeList=$secondResult[2];
$secondNameList=$secondResult[4];
foreach($secondNameListas$m=$n){
$iscun=$ModelObj-where(array('name'=iconv('GB2312','UTF-8',$n),'parent_id'=$first_re))-find();
if(!$iscun){
$adddata=array(
'code'=$secondCodeList[$m],
'name'=iconv('GB2312','UTF-8',$n),
'parent_id'=$first_re,
);
$second_re=$ModelObj-add($adddata);
if($second_re){
//第
$thirdDetail=file_get_contents($baseUrl.$urllist[$k].'/'.$secondUrlList[$m].'.html');
$thirdPattern='/td(?:ahref=\'.*?\')*(\d+?)(?:\/a)*\/tdtd(?:ahref=\'.*?\')*(.*?)(?:\/a)*\/td/i';
preg_match_all($thirdPattern,$thirdDetail,$thirdResult);
$thirdCodeList=$thirdResult[1];
$thirdNameList=$thirdResult[2];
foreach($thirdNameListas$p=$q){
$iscun=$ModelObj-where(array('name'=iconv('GB2312','UTF-8',$q),'parent_id'=$second_re))-find();
if(!$iscun){
$adddata=array(
'code'=$thirdCodeList[$p],
'name'=iconv('GB2312','UTF-8',$q),
'parent_id'=$second_re,
);
$third_re=$ModelObj-add($adddata);
}
}
}
}
}
}
}
}
}
2.省级代码替换
/**
*
*/
publicfunctionprovincereplace(){
//省级code
$provinceCode=array(
array('code'='110000','title'='北京市'),
array('code'='120000','title'='天津市'),
array('code'='130000','title'='河北省'),
array('code'='140000','title'='山西省'),
array('code'='150000','title'='内蒙古自治区'),
array('code'='210000','title'='辽宁省'),
array('code'='220000','title'='吉林省'),
array('code'='230000','title'='黑龙江省'),
array('code'='310000','title'='上海市'),
array('code'='320000','title'='江苏省'),
array('code'='330000','title'='浙江省'),
array('code'='340000','title'='安徽省'),
array('code'='350000','title'='福建省'),
array('code'='360000','title'='江西省'),
array('code'='370000','title'='山东省'),
array('code'='410000','title'='河南省'),
array('code'='420000','title'='湖北省'),
array('code'='430000','title'='湖南省'),
array('code'='440000','title'='广东省'),
array('code'='450000','title'='广西壮族自治区'),
array('code'='460000','title'='海南省'),
array('code'='500000','title'='重庆市'),
array('code'='510000','title'='四川省'),
array('code'='520000','title'='贵州省'),
array('code'='530000','title'='云南省'),
array('code'='540000','title'='西藏自治区'),
array('code'='610000','title'='陕西省'),
array('code'='620000','title'='甘肃省'),
array('code'='630000','title'='青海省'),
array('code'='640000','title'='宁夏回族自治区'),
array('code'='650000','title'='新疆维吾尔自治区'),
);
$ModelObj=D('Areas');
foreach($provinceCodeas$k=$v){
$ModelObj-where(array('name'=$v['title']))-setField('code',$v['code']);
}
}
3.处理级别父级代码
/**
*处理级别父级代码
*/
publicfunctionlevelandparentcode(){
$ModelObj=D('Areas');
$alllist=$ModelObj-select();
foreach($alllistas$k=$v){
if(!$v['parent_id']){
$data=array(
'level'=1,
'parent_code'='0',
);
}else{
$parent=$ModelObj-where(array('id'=$v['parent_id']))-find();
if(!$parent['parent_id']){
$data=array(
'level'=2,
'parent_code'=$parent['code'],
);
}else{
$data=array(
'level'=3,
'parent_code'=$parent['code'],
);
}
}
$re=$ModelObj-where(array('id'=$v['id']))-save($data);
}
}
4.有了的数据做联动就很简单。
formid=searce
pclass=screen-li
selectname=provinceid=province
optionvalue=省/option
volistname=provinceid=item!--循环省数据--
optionvalue={$item.code}{$item.name}/option
/volist
/select
/
pclass=screen-li
selectname='city'id=city
optionvalue=市/option
/select
/
pclass=screen-li
selectname='area'id=area
optionvalue=区/县/option
/select
/
script
$('#province).change(function(){
id=$(this).val();
get_city(id)
})
functionget_city(id){
url_get_city={:U('city')}?id=+id
$(#city).load(url_get_city,{n:Math.random()});
}
$('#city).change(function(){
id=$(this).val();
get_area(id)
})
functionget_area(id){
url_get_area={:U('area')}?id=+id
$(#area).load(url_get_area,{n:Math.random()});
}
/script
后台只要通过id查询出该级别下的数据就可以