rabin 3 jaren geleden
bovenliggende
commit
91abfd6b05
1 gewijzigde bestanden met toevoegingen van 15 en 11 verwijderingen
  1. 15 11
      src/Import.php

+ 15 - 11
src/Import.php

@@ -6,7 +6,7 @@ use Dever;
 
 class Import
 {
-    private $url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2021/';
+    private $url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2020/';
 	/**
      * 获取国家统计局最新的地区数据
      *
@@ -33,7 +33,7 @@ class Import
     public function getProvince($result)
     {
         $update = array();
-        if (isset($result[1]) && isset($result[2])) {
+        if (isset($result[1]) && isset($result[2]) && $result[2]) {
             foreach ($result[2] as $k => $v) {
                 $update['id'] = $this->id(trim($result[1][$k], '.html'));
                 $update['name'] = strip_tags($v);
@@ -41,7 +41,10 @@ class Import
                 $id = Dever::upinto('area/province', $update, $update);
 
                 # 获取城市
-                $this->getCity($id, $update['name'], $result[1][$k]);
+                if ($update['name'] == '广东省') {
+                    $this->getCity($id, $update['name'], $result[1][$k]);
+                }
+                
             }
         }
     }
@@ -57,7 +60,7 @@ class Import
         preg_match_all('/<tr class=\'citytr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
 
         $update = array();
-        if (isset($result[3]) && isset($result[4])) {
+        if (isset($result[3]) && isset($result[4]) && $result[4]) {
             foreach ($result[4] as $k => $v) {
                 $v = strip_tags($v);
                 if ($v == '市辖区') {
@@ -76,11 +79,11 @@ class Import
         }
     }
 
-    public function getCounty($province, $city, $link)
+    public function getCounty($province, $city, $source_link)
     {
-        $url = $this->url . $link;
+        $url = $this->url . $source_link;
 
-        $temp = explode('/', $link);
+        $temp = explode('/', $source_link);
         $link = $temp[0];
         
         $html = Dever::curl($url);
@@ -90,7 +93,7 @@ class Import
         preg_match_all('/<tr class=\'countytr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
 
         $update = array();
-        if (isset($result[3]) && isset($result[4])) {
+        if (isset($result[3]) && isset($result[4]) && $result[4]) {
             foreach ($result[4] as $k => $v) {
                 $update['id'] = $this->id($result[2][$k]);
                 $update['name'] = strip_tags($v);
@@ -107,7 +110,7 @@ class Import
         } else {
             $city_info = Dever::db('area/city')->find(array('id' => $city, 'clear' => true));
             $update['id'] = $city_info['id'];
-            $update['name'] = $city_info['name'];
+            $update['name'] = $city_info['name'] . '辖区';
             $update['city_id'] = $city;
             $update['province_id'] = $province;
             $update['area'] = $province . ',' . $city;
@@ -115,10 +118,11 @@ class Import
             $update['level'] = 1;
             $update['pinyin'] = $city_info['pinyin'];
             $update['pinyin_first'] = $city_info['pinyin_first'];
+
             $id = Dever::upinto('area/county', $update, $update);
 
             # 获取街道
-            $this->getTown($province, $city, $id, false, $html);
+            $this->getTown($province, $city, $id, $source_link, $html);
         }
     }
 
@@ -141,7 +145,7 @@ class Import
         preg_match_all('/<tr class=\'towntr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
 
         $update = array();
-        if (isset($result[3]) && isset($result[4])) {
+        if (isset($result[3]) && isset($result[4]) && $result[4]) {
             foreach ($result[4] as $k => $v) {
                 $update['id'] = $this->id($result[2][$k], 9);
                 $update['name'] = strip_tags($v);