找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 23|回复: 1

[文本处理] 求助BAT或PowerShell获取国家地名信息库的行政区划信息

[复制链接]
发表于 2 小时前 | 显示全部楼层 |阅读模式
待获取的数据来源:https://dmfw.mca.gov.cn/XzqhVersionPublish.html
获取4个字段:L1、L2、L3、行政区划代码

  • 直辖市:获取到区级别,不需要获取街道级别
  • 省:获取到区县级别,不需要获取街道、乡镇级别


输出格式:csv或xlsx

举例:
20260608-1.jpg
发表于 1 小时前 | 显示全部楼层
  1. $ErrorActionPreference = "Stop"

  2. $Api = "https://dmfw.mca.gov.cn/9095/xzqh/getList"
  3. $OutFile = "D:\xzqh.csv"

  4. # Beijing, Tianjin, Shanghai, Chongqing
  5. $MunicipalCodes = @("110000", "120000", "310000", "500000")

  6. function Get-Code6 {
  7.     param($Code)

  8.     if ($null -eq $Code) {
  9.         return ""
  10.     }

  11.     $s = [string]$Code

  12.     if ($s -match "^\d{6}") {
  13.         return $matches[0]
  14.     }

  15.     return $s
  16. }

  17. function Get-Children {
  18.     param($Node)

  19.     if ($null -eq $Node) {
  20.         return @()
  21.     }

  22.     $names = $Node.PSObject.Properties.Name

  23.     foreach ($p in @("children", "childList", "list")) {
  24.         if ($names -contains $p) {
  25.             if ($null -ne $Node.$p) {
  26.                 return @($Node.$p)
  27.             }
  28.         }
  29.     }

  30.     return @()
  31. }

  32. function Get-RootList {
  33.     param($Json)

  34.     if ($null -ne $Json.response.data.children) {
  35.         return @($Json.response.data.children)
  36.     }

  37.     if ($null -ne $Json.data.children) {
  38.         return @($Json.data.children)
  39.     }

  40.     if ($null -ne $Json.children) {
  41.         return @($Json.children)
  42.     }

  43.     if ($null -ne $Json.data) {
  44.         return @($Json.data)
  45.     }

  46.     throw "Can not find root list."
  47. }

  48. function Invoke-Xzqh {
  49.     param(
  50.         [string]$Code = "",
  51.         [int]$MaxLevel = 3
  52.     )

  53.     $encCode = [uri]::EscapeDataString($Code)
  54.     $url = $Api + "?code=" + $encCode + "&maxLevel=" + $MaxLevel

  55.     Invoke-RestMethod `
  56.         -Method Get `
  57.         -Uri $url `
  58.         -Headers @{
  59.             "User-Agent" = "Mozilla/5.0"
  60.             "Accept" = "application/json"
  61.         } `
  62.         -TimeoutSec 90
  63. }

  64. $Rows = New-Object System.Collections.Generic.List[object]

  65. function Add-Row {
  66.     param(
  67.         [string]$L1,
  68.         [string]$L2,
  69.         [string]$L3,
  70.         $Code
  71.     )

  72.     $code6 = Get-Code6 $Code

  73.     $Rows.Add([pscustomobject]@{
  74.         L1 = $L1
  75.         L2 = $L2
  76.         L3 = $L3
  77.         "行政区划代码" = $code6
  78.     })
  79. }

  80. Write-Host "Downloading..."

  81. $json = Invoke-Xzqh -Code "" -MaxLevel 3
  82. $provinces = Get-RootList $json

  83. foreach ($p in $provinces) {
  84.     $pName = [string]$p.name
  85.     $pCode6 = Get-Code6 $p.code

  86.     if ([string]::IsNullOrWhiteSpace($pName)) {
  87.         continue
  88.     }

  89.     Add-Row $pName "" "" $p.code

  90.     $pChildren = Get-Children $p

  91.     if ($MunicipalCodes -contains $pCode6) {
  92.         foreach ($d in $pChildren) {
  93.             $dName = [string]$d.name

  94.             if ([string]::IsNullOrWhiteSpace($dName)) {
  95.                 continue
  96.             }

  97.             Add-Row $pName $dName "" $d.code
  98.         }

  99.         continue
  100.     }

  101.     foreach ($c in $pChildren) {
  102.         $cName = [string]$c.name

  103.         if ([string]::IsNullOrWhiteSpace($cName)) {
  104.             continue
  105.         }

  106.         Add-Row $pName $cName "" $c.code

  107.         $districts = Get-Children $c

  108.         foreach ($d in $districts) {
  109.             $dName = [string]$d.name

  110.             if ([string]::IsNullOrWhiteSpace($dName)) {
  111.                 continue
  112.             }

  113.             Add-Row $pName $cName $dName $d.code
  114.         }
  115.     }
  116. }

  117. $csv = $Rows | ConvertTo-Csv -NoTypeInformation
  118. $utf8Bom = New-Object System.Text.UTF8Encoding $true
  119. [System.IO.File]::WriteAllLines($OutFile, $csv, $utf8Bom)

  120. Write-Host ("Done: " + $OutFile)
  121. Write-Host ("Rows: " + $Rows.Count)
复制代码

被AI秒了...第一次报错,回复说是编码搞坏了,第2次就是这个代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-6-8 18:21

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表