代码拉取完成,页面将自动刷新
# -*- 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("写入完成")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。