Last active
January 21, 2021 13:04
-
-
Save bookiu/43873178ab6b6347b0466d0c0a262780 to your computer and use it in GitHub Desktop.
多级省市区嵌套实现
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
func main() { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = file_get_contents("http://preview.www.mca.gov.cn/article/sj/xzqh/2020/2020/202101041104.html"); | |
preg_match_all('#<td class=xl\d+?>(<span.*?)?(\d+|[\x{4e00}-\x{9fa5}]+).*?</td>#u', $data, $matches); | |
$administratives = []; | |
$node = array( | |
'id' => '', | |
'name' => '' | |
); | |
$fixNode = array( | |
'id' => '', | |
'name' => '市辖区' | |
); | |
foreach ($matches[2] as $idx => $v) { | |
if ($idx % 2 == 0) { | |
$node = array( | |
'id' => $v, | |
'name' => '' | |
); | |
if (in_array($v, ['110000', '120000', '310000', '500000'])) { | |
$v[3] = '1'; | |
$fixNode['id'] = $v; | |
} | |
} else if ($idx % 2 == 1) { | |
$node['name'] = $v; | |
$administratives[] = $node; | |
if (in_array($v, ['北京市', '天津市', '上海市', '重庆市'])) { | |
$administratives[] = $fixNode; | |
} | |
} | |
} | |
unset($node); | |
$root = array(); | |
$nodeMap = array(); | |
foreach ($administratives as $item) { | |
$rawId = (string)$item['id']; | |
$id = (int)$rawId; | |
$provinceId = (int)($id / 10000); | |
$cityId = ((int)($id / 100)) % 100; | |
$districtId = $id % 100; | |
if ($cityId === 0 && $districtId === 0) { | |
// 省份信息 | |
$node = array( | |
'Id' => $rawId, | |
'Name' => $item['name'], | |
'Cities' => [] | |
); | |
$nodeMap[$id] = &$node; | |
$root[] = &$node; | |
} else if ($districtId === 0) { | |
// 市信息 | |
$fullCityId = $provinceId * 10000 + $cityId * 100; | |
$node = array( | |
'Id' => $rawId, | |
'Name' => $item['name'], | |
'Districts' => [] | |
); | |
$nodeMap[$fullCityId] = &$node; | |
$provinceNode = &$nodeMap[$provinceId * 10000]; | |
$provinceNode['Cities'][] = &$node; | |
} else { | |
// 区信息 | |
$fullCityId = $provinceId * 10000 + $cityId * 100; | |
$node = array( | |
'Id' => $rawId, | |
'Name' => $item['name'], | |
); | |
$cityNode = &$nodeMap[$fullCityId]; | |
$cityNode['Districts'][] = $node; | |
} | |
unset($node); | |
} | |
print_r($root); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment