网站上有很多jq的三级联动,首如果都会地区数据,那我们如何获得到这些数据并且保留在我们的数据表中,
营销型网站设计,
这是国度统计局的数据,下面我们用PHP按次将他们收罗上去
1.数据收罗
public function countryaddress() {
$ModelObj = D('Areas');
$baseUrl = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/';
//第一级
$firstDetail = file_get_contents($baseUrl . 'index.html');
$firstPattern = '/
(.*?)
<\/a><\/td>/i';
preg_match_all($firstPattern, $firstDetail, $firstResult);
$urllist = $firstResult[1];
$namelist = $firstResult[2];
foreach ($namelist as $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 = "/(\d+?)<\/a><\/td>(.*?)<\/a>/i";
preg_match_all($secondPattern, $secondDetail, $secondResult);
$secondUrlList = $secondResult[1];
$secondCodeList = $secondResult[2];
$secondNameList = $secondResult[4];
foreach ($secondNameList as $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 = '/(?:)*(\d+?)(?:<\/a>)*<\/td>(?:)*(.*?)(?:<\/a>)*<\/td>/i';
preg_match_all($thirdPattern, $thirdDetail,