包含中国省、市、县、街道、村/居委会5级数据(港、澳、台除外(统计局官网没有给出)),数据来源为国家统计局。
程序基于tp3.2.3编写,依赖Querylist3。
主要代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | /** * @author Adam * Time: 2018/6/15 14:50 */ public function sheng() { $data = QueryList::Query( $this ->baseurl . "index.html" , array ( "url" => array ( "td>a " , "href" ), "name" => array ( "td>a" , "text" )), "" , 'UTF-8' , 'GB2312' )->getData(); foreach ( $data as $k => $v ) { $data [ $k ][ "url" ] = str_replace ( ".html" , "" , $v [ "url" ]); $data [ $k ][ "saveid" ] = $this ->generate_full_id( $data [ $k ][ "url" ]); $res = $this ->areamodel->where( array ( "area_id" => $data [ $k ][ "url" ]))->find(); if ( empty ( $res )) { $this ->areamodel->data( array ( "area_id" => $data [ $k ][ "url" ], "area_pid" => 0, "area_name" => $data [ $k ][ "name" ], "level" => 1))->add(); } } echo json_encode( $data ); die ; } /** * 二级 市 http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/14.html * @author Adam * Time: 2018/6/20 13:43 */ public function secondway() { $toplevel = $this ->areamodel->where( array ( "imported" => 0, "level" => 1))->order( "area_id DESC" )->find(); if ( empty ( $toplevel )) { echo "全部导入完毕,无需重新导入" ; die ; } $fatherid = $toplevel [ "area_id" ]; $url = $this ->baseurl . $toplevel [ "area_id" ] . ".html" ; $data = QueryList::Query( $url , array ( "url" => array ( "td:eq(0)>a" , "href" ), "name" => array ( "td:eq(1)>a" , "text" )), "tr.citytr" , 'UTF-8' , 'GB2312' )->getData(); foreach ( $data as $k => $v ) { $data [ $k ][ "url" ] = str_replace ( ".html" , "" , $this ->reg_real_id( $v [ "url" ])[0]); } M()->startTrans(); foreach ( $data as $k1 => $v1 ) { $res [ "area_id" ] = $v1 [ "url" ]; $res [ "area_pid" ] = $toplevel [ "area_id" ]; $res [ "level" ] = 2; $res [ "area_name" ] = $v1 [ "name" ]; if (! $this ->areamodel->data( $res )->add()) { echo "循环导入失败" . M()->getLastSql(); die ; M()->rollback(); } } $this ->areamodel->where( array ( "area_id" => $fatherid ))->save( array ( "imported" => 1)); $sql = M()->getLastSql(); M()->commit(); echo "导入成功 " . $sql ; die ; } /** * 三级 区/县(市) * @author Adam * Time: 2018/6/21 15:34 */ public function thirdway() { $secondlevel = $this ->areamodel->where( array ( "imported" => 0, "level" => 2))->order( "area_id DESC" )->find(); if ( empty ( $secondlevel )) { echo "区/县全部导入完毕,无需重新导入" ; die ; } $topid = $secondlevel [ "area_pid" ]; $fatherid = $secondlevel [ "area_id" ]; $url = $this ->baseurl . $topid . "/" . $fatherid . ".html" ; $data = QueryList::Query( $url , array ( "url" => array ( "td:eq(0)>a" , "href" ), "name" => array ( "td:eq(1)" , "text" ), "extra" => array ( "td:eq(0)" , "text" )), "tr.countytr" , 'UTF-8' , 'GB2312' )->getData(); foreach ( $data as $k => $v ) { $data [ $k ][ "url" ] = str_replace ( ".html" , "" , $this ->reg_real_id( $v [ "url" ])[0]); if ( empty ( $data [ $k ][ "url" ])) { $data [ $k ][ "url" ] = $this ->spit_pure_id( $v [ "extra" ]); //防止空数据 } } // echo json_encode($data);die; M()->startTrans(); foreach ( $data as $k => $v ) { $res [ "area_id" ] = $v [ "url" ]; $res [ "area_pid" ] = $secondlevel [ "area_id" ]; $res [ "level" ] = 3; $res [ "area_name" ] = $v [ "name" ]; if (! $this ->areamodel->data( $res )->add()) { echo "循环导入失败" . M()->getLastSql(); die ; M()->rollback(); } } if ( $this ->areamodel->where( array ( "area_id" => $fatherid ))->save( array ( "imported" => 1)) == false) { echo "更新父类失败" ; M()->rollback(); die ; }; $sql = M()->getLastSql(); M()->commit(); echo "导入成功" . $sql ; die ; } /**四级 办事处/镇 * @author Adam * Time: 2018/6/21 15:35 */ public function fourthway() { $thirdlevel = $this ->areamodel->where( array ( "imported" => 0, "level" => 3))->order( "area_id DESC" )->find(); if ( empty ( $thirdlevel )) { echo "办事处/镇全部导入完毕,无需重新导入" ; die ; } $fatherid = $thirdlevel [ "area_id" ]; $secondid = $thirdlevel [ "area_pid" ]; $secondmeta = $this ->areamodel->where( array ( "area_id" => $secondid ))->find(); $topid = $secondmeta [ "area_pid" ]; $url = $this ->baseurl . $topid . "/" . str_replace ( $topid , "" , $secondid ) . "/" . $fatherid . ".html" ; $data = QueryList::Query( $url , array ( "url" => array ( "td:eq(0)>a" , "href" ), "name" => array ( "td:eq(1)" , "text" ), "extra" => array ( "td:eq(0)" , "text" )), "tr.towntr" , 'UTF-8' , 'GB2312' )->getData(); foreach ( $data as $k => $v ) { $data [ $k ][ "url" ] = str_replace ( ".html" , "" , $this ->reg_real_id( $v [ "url" ])[0]); if ( empty ( $data [ $k ][ "url" ])) { $data [ $k ][ "url" ] = $this ->spit_pure_id( $v [ "extra" ]); //防止空数据 } } M()->startTrans(); foreach ( $data as $k => $v ) { $res [ "area_id" ] = $v [ "url" ]; $res [ "area_pid" ] = $thirdlevel [ "area_id" ]; $res [ "level" ] = 4; $res [ "area_name" ] = $v [ "name" ]; if (! $this ->areamodel->data( $res )->add()) { echo "循环导入失败" . M()->getLastSql(); die ; M()->rollback(); } } if ( $this ->areamodel->where( array ( "area_id" => $fatherid ))->save( array ( "imported" => 1)) == false) { echo "更新父类失败" ; M()->rollback(); die ; }; $sql = M()->getLastSql(); M()->commit(); echo "导入成功" . $sql ; die ; } /**五级 居委会/村委会 * @author Adam * Time: 2018/6/21 15:37 */ public function fifthway() { $fourthlevel = $this ->areamodel->where( array ( "imported" => 0, "level" => 4))->order( "area_id DESC" )->find(); if ( empty ( $fourthlevel )) { echo "办事处/镇全部导入完毕,无需重新导入" ; die ; } $fatherid = $fourthlevel [ "area_id" ]; $thirdid = $fourthlevel [ "area_pid" ]; $thirdmeta = $this ->areamodel->where( array ( "area_id" => $thirdid ))->find(); $secondid = $thirdmeta [ "area_pid" ]; $secondmeta = $this ->areamodel->where( array ( "area_id" => $secondid ))->find(); $topid = $secondmeta [ "area_pid" ]; $url = $this ->baseurl . $topid . "/" . str_replace ( $topid , "" , $secondid ) . "/" . str_replace ( $secondid , "" , $thirdid ) . "/" . $fatherid . ".html" ; $data = QueryList::Query( $url , array ( "url" => array ( "td:eq(0)" , "text" ), "name" => array ( "td:eq(2)" , "text" ), "extra" => array ( "td:eq(0)" , "text" )), "tr.villagetr" , 'UTF-8' , 'GB2312' )->getData(); foreach ( $data as $k => $v ) { $data [ $k ][ "url" ] = str_replace ( ".html" , "" , $this ->reg_real_id( $v [ "url" ])[0]); if ( empty ( $data [ $k ][ "url" ])) { $data [ $k ][ "url" ] = $this ->spit_pure_id( $v [ "extra" ]); //防止空数据 } } M()->startTrans(); foreach ( $data as $k => $v ) { $res [ "area_id" ] = $v [ "url" ]; $res [ "area_pid" ] = $fourthlevel [ "area_id" ]; $res [ "level" ] = 5; $res [ "area_name" ] = $v [ "name" ]; if (! $this ->areamodel->data( $res )->add()) { echo "循环导入失败111" . M()->getLastSql(); die ; M()->rollback(); } } if ( $this ->areamodel->where( array ( "area_id" => $fatherid ))->save( array ( "imported" => 1)) == false) { echo "更新父类失败" ; M()->rollback(); die ; }; $sql = M()->getLastSql(); M()->commit(); echo "导入成功" . $sql ; die ; } /** * 生成完整的12位id * @param $id * @return string * @author Adam * Time: 2018/6/20 17:19 */ private function generate_full_id( $id ) { if ( strlen ( $id ) > 12) { return $id ; } else { $needzerecount = 12 - strlen ( $id ); for ( $i = 0; $i < $needzerecount ; $i ++) { $id .= "0" ; } return $id ; } } /** * 去除字符转后面的0 * @param $id * @return null|string|string[] * @author Adam * Time: 2018/6/20 13:02 */ private function spit_pure_id( $id ) { $matches = preg_replace( "/0+$/" , "" , $id ); return $matches ; } /** * 正则匹配id * @param $id * @return mixed * @author Adam * Time: 2018/6/20 17:22 */ public function reg_real_id( $id ) { preg_match( "/\d+.html$/" , $id , $matches ); return $matches ; } |
github地址: https://github.com/adamin1990/China_locations_spider_database 包含程序源代码和采集的五级国家省市区数据库文件。
本文为Adamin90原创文章,转载无需和我联系,但请注明来自http://www.lixiaopeng.top
sunnier:
啥子东西这是
2018-06-22 17:32:24 回复