forked from lyhmyd1211/GeoMapData_CN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getMap.py
72 lines (67 loc) · 2.73 KB
/
getMap.py
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
# -*- 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("写入完成")