Import.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace Area\Api;
  3. set_time_limit(0);
  4. use Dever;
  5. class Import
  6. {
  7. private $url = 'https://www.stats.gov.cn/sj/tjbz/tjyqhdmhcxhfdm/2023/';
  8. public function getUrl()
  9. {
  10. return $this->url;
  11. }
  12. /**
  13. * 获取国家统计局最新的地区数据
  14. *
  15. * @return mixed
  16. */
  17. public function load()
  18. {
  19. $url = $this->url . 'index.html';
  20. $html = $this->html($url);
  21. preg_match_all('/<td><a href="(.*?)">(.*?)<br \/><\/a><\/td>/i', $html, $result);
  22. # 获取省份
  23. $this->getProvince($result);
  24. return 1;
  25. }
  26. public function getProvince($result)
  27. {
  28. $province = Dever::input('province');
  29. $update = array();
  30. if (isset($result[1]) && isset($result[2]) && $result[2]) {
  31. foreach ($result[2] as $k => $v) {
  32. $update['id'] = $this->id(trim($result[1][$k], '.html'));
  33. $update['name'] = strip_tags($v);
  34. $update = Dever::load('data', 'area')->pinyin($update);
  35. $id = $this->up('province', $update['id'], $update);
  36. # 获取城市
  37. if ($province) {
  38. if ($update['name'] == $province) {
  39. $this->getCity($id, $update['name'], $result[1][$k]);
  40. }
  41. } else {
  42. $this->getCity($id, $update['name'], $result[1][$k]);
  43. }
  44. }
  45. }
  46. }
  47. public function getCity($province, $province_name, $link)
  48. {
  49. $city = Dever::input('city');
  50. $url = $this->url . $link;
  51. $html = $this->html($url);
  52. preg_match_all('/<tr class="citytr"><td><a href="(.*?)">(.*?)<\/a><\/td><td><a href="(.*?)">(.*?)<\/a><\/td><\/tr>/is', $html, $result);
  53. $update = array();
  54. if (isset($result[3]) && isset($result[4]) && $result[4]) {
  55. foreach ($result[4] as $k => $v) {
  56. $v = strip_tags($v);
  57. if ($v == '市辖区') {
  58. $v = $province_name;
  59. }
  60. $update['id'] = $this->id($result[2][$k]);
  61. $update['name'] = $v;
  62. $update['province_id'] = $province;
  63. $update = Dever::load('data', 'area')->pinyin($update);
  64. $id = $this->up('city', $update['id'], $update);
  65. if ($city) {
  66. if ($update['name'] == $city) {
  67. $this->getCounty($province, $id, $result[3][$k]);
  68. }
  69. } else {
  70. $this->getCounty($province, $id, $result[3][$k]);
  71. }
  72. }
  73. }
  74. }
  75. public function getCounty($province, $city, $source_link)
  76. {
  77. $url = $this->url . $source_link;
  78. $temp = explode('/', $source_link);
  79. $link = $temp[0];
  80. $html = $this->html($url);
  81. preg_match_all('/<tr class="countytr"><td><a href="(.*?)">(.*?)<\/a><\/td><td><a href="(.*?)">(.*?)<\/a><\/td><\/tr>/i', $html, $result);
  82. $update = array();
  83. if (isset($result[3]) && isset($result[4]) && $result[4]) {
  84. foreach ($result[4] as $k => $v) {
  85. $update['id'] = $this->id($result[2][$k]);
  86. $update['name'] = strip_tags($v);
  87. $update['city_id'] = $city;
  88. $update['province_id'] = $province;
  89. $update['area'] = $province . ',' . $city;
  90. $this->setLevelCounty($update);
  91. $update = Dever::load('data', 'area')->pinyin($update);
  92. $id = $this->up('county', $update['id'], $update);
  93. # 获取街道
  94. $this->getTown($province, $city, $id, $link . '/' . $result[3][$k]);
  95. }
  96. } else {
  97. $city_info = Dever::db('city')->find($city);
  98. $update['id'] = $city_info['id'];
  99. $update['name'] = $city_info['name'] . '辖区';
  100. $update['city_id'] = $city;
  101. $update['province_id'] = $province;
  102. $update['area'] = $province . ',' . $city;
  103. $update['type'] = 1;
  104. $update['level'] = 1;
  105. $update['pinyin'] = $city_info['pinyin'];
  106. $update['pinyin_first'] = $city_info['pinyin_first'];
  107. $id = $this->up('county', $update['id'], $update);
  108. # 获取街道
  109. $this->getTown($province, $city, $id, $source_link, $html);
  110. }
  111. }
  112. public function getTown($province, $city, $county, $link = false, $html = false)
  113. {
  114. if ($link) {
  115. $url = $this->url . $link;
  116. $temp = explode('/', $link);
  117. $link = $temp[0] . '/' . $temp[1];
  118. $html = $this->html($url);
  119. }
  120. if (!$link && !$html) {
  121. return;
  122. }
  123. preg_match_all('/<tr class="towntr"><td><a href="(.*?)">(.*?)<\/a><\/td><td><a href="(.*?)">(.*?)<\/a><\/td><\/tr>/i', $html, $result);
  124. $update = array();
  125. if (isset($result[3]) && isset($result[4]) && $result[4]) {
  126. foreach ($result[4] as $k => $v) {
  127. $update['id'] = $this->id($result[2][$k], 9);
  128. $update['name'] = strip_tags($v);
  129. $update['county_id'] = $county;
  130. $update['city_id'] = $city;
  131. $update['province_id'] = $province;
  132. $update['area'] = $province . ',' . $city . ',' . $county;
  133. $update = Dever::load('data', 'area')->pinyin($update);
  134. $id = $this->up('town', $update['id'], $update);
  135. # 获取社区
  136. //$this->getVillage($province, $city, $county, $id, $link . '/' . $result[3][$k]);
  137. }
  138. }
  139. }
  140. public function getVillage($province, $city, $county, $town, $link)
  141. {
  142. $url = $this->url . $link;
  143. $html = $this->html($url);
  144. preg_match_all('/<tr class="villagetr"><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><\/tr>/i', $html, $result);
  145. $update = array();
  146. if (isset($result[1]) && isset($result[2]) && isset($result[3])) {
  147. foreach ($result[3] as $k => $v) {
  148. $update['id'] = $this->id($result[1][$k], 12);
  149. $update['code'] = $result[2][$k];
  150. $update['name'] = strip_tags($v);
  151. $update['town_id'] = $town;
  152. $update['county_id'] = $county;
  153. $update['city_id'] = $city;
  154. $update['province_id'] = $province;
  155. $update['area'] = $province . ',' . $city . ',' . $county . ',' . $town;
  156. $update = Dever::load('data', 'area')->pinyin($update);
  157. $this->up('village', $update['id'], $update);
  158. }
  159. }
  160. }
  161. public function id($id, $len = 6)
  162. {
  163. $id = substr($id, 0, $len);
  164. $id = str_pad($id, $len, '0', STR_PAD_RIGHT);
  165. return $id;
  166. }
  167. public function setLevelCounty(&$update)
  168. {
  169. $num = substr($update['id'], 4);
  170. # type = 1城区 2郊区 3县城 4经济技术开发 5县级市
  171. if ($update['name'] == '门头沟区') {
  172. $update['type'] = 2;
  173. $update['level'] = 2;
  174. } elseif ($num <= 10) {
  175. $update['type'] = 1;
  176. $update['level'] = 1;
  177. } elseif ($num > 10 && $num <= 20) {
  178. $update['type'] = 2;
  179. $update['level'] = 2;
  180. } elseif ($num > 20 && $num <= 70) {
  181. $update['type'] = 3;
  182. $update['level'] = 3;
  183. } elseif ($num > 70 && $num <= 80) {
  184. $update['type'] = 4;
  185. $update['level'] = 2;
  186. } elseif ($num >= 80) {
  187. $update['type'] = 5;
  188. $update['level'] = 2;
  189. }
  190. }
  191. public function up($table, $id, $data)
  192. {
  193. $db = Dever::db($table, 'area');
  194. $info = $db->find($id);
  195. if (!$info) {
  196. $db->insert($data);
  197. } else {
  198. $db->update($info['id'], $data);
  199. }
  200. return $id;
  201. }
  202. private function html($url)
  203. {
  204. $html = Dever::curl($url)->result();
  205. //$html = Dever::convert($html, "UTF-8", "GBK");
  206. $html = preg_replace('//', '', $html); // 去掉HTML注释
  207. $html = preg_replace('/\s+/', ' ', $html); // 清除多余的空格
  208. $html = preg_replace('/>\s</', '><', $html); // 去掉标记之间的空格
  209. return $html;
  210. }
  211. }