1 Star 11 Fork 10

webxw3435/GeoJson

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
getMap.py 2.73 KB
一键复制 编辑 原始数据 按行查看 历史
是小王啊i 提交于 1年前 . geojsondata
# -*- coding: UTF-8 -*-
import json
import urllib.request
import urllib.parse
import ssl
import os
def getAreaCodeType(code):
if code[2:6] == "0000":
return "1"
elif code[4:6] == "00":
return "2"
else:
return "3"
if not os.path.isdir("province"):
os.mkdir("province")
print("创建province文件夹")
if not os.path.isdir("citys"):
os.mkdir("citys")
print("创建citys文件夹")
if not os.path.isdir("county"):
os.mkdir("county")
print("创建county文件夹")
context = ssl._create_unverified_context()
url = "https://geo.datav.aliyun.com/areas_v2/bound/infos.json"
with urllib.request.urlopen(url, context=context) as response:
print("请求接口并写入文件中。。。")
html = json.loads(response.read().decode("UTF-8"))
with open("location.json", "w", encoding='utf-8') as json_file:
json.dump(html, json_file, ensure_ascii=False)
print("写入 location.json")
for item in list(html.keys()):
if getAreaCodeType(item) == "3":
with urllib.request.urlopen(
"https://geo.datav.aliyun.com/areas_v2/bound/" + item + ".json",
context=context,
) as res:
print(
"请求:https://geo.datav.aliyun.com/areas_v2/bound/" + item + ".json"
)
text = json.loads(res.read().decode("UTF-8"))
with open("county/" + item + ".json", "w", encoding='utf-8') as json_file:
json.dump(text, json_file, ensure_ascii=False)
print("写入", item)
else:
with urllib.request.urlopen(
"https://geo.datav.aliyun.com/areas_v2/bound/" + item + "_full.json",
context=context,
) as res:
print(
"请求:https://geo.datav.aliyun.com/areas_v2/bound/"
+ item
+ "_full.json"
)
text = json.loads(res.read().decode("UTF-8"))
if item == "100000":
with open("china.json", "w", encoding='utf-8') as json_file:
json.dump(text, json_file, ensure_ascii=False)
print("写入", item)
elif getAreaCodeType(item) == "1":
with open("province/" + item + ".json", "w", encoding='utf-8') as json_file:
json.dump(text, json_file, ensure_ascii=False)
print("写入", item)
elif getAreaCodeType(item) == "2":
with open("citys/" + item + ".json", "w", encoding='utf-8') as json_file:
json.dump(text, json_file, ensure_ascii=False)
print("写入", item)
print("写入完成")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/web_xw3435/geo-json.git
git@gitee.com:web_xw3435/geo-json.git
web_xw3435
geo-json
GeoJson
master

搜索帮助