Web.php 7.0 KB

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