Web.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php namespace Area\Lib\Import;
  2. set_time_limit(0);
  3. use Dever;
  4. use Area\Lib\Data;
  5. /**
  6. * 获取国家统计局最新的地区数据
  7. *
  8. * @return mixed
  9. */
  10. class Web extends Core
  11. {
  12. private $url = 'https://www.stats.gov.cn/sj/tjbz/tjyqhdmhcxhfdm/2023/';
  13. public function getUrl()
  14. {
  15. return $this->url;
  16. }
  17. public function get()
  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 = [];
  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::class)->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 = [];
  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::class)->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 = [];
  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::class)->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('area/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 = [];
  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::class)->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 = [];
  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::class)->pinyin($update);
  157. $this->up('village', $update['id'], $update);
  158. }
  159. }
  160. }
  161. private function html($url)
  162. {
  163. $html = Dever::curl($url)->result();
  164. //$html = Dever::convert($html, "UTF-8", "GBK");
  165. $html = preg_replace('//', '', $html); // 去掉HTML注释
  166. $html = preg_replace('/\s+/', ' ', $html); // 清除多余的空格
  167. $html = preg_replace('/>\s</', '><', $html); // 去掉标记之间的空格
  168. return $html;
  169. }
  170. }