python获取全国geojson,省市区县

数据来源
https://datav.aliyun.com/portal/school/atlas/area_selector

# 请求库

import requests

# 解析库

# from bs4 import BeautifulSoup

# 用于解决爬取的数据格式化

import io

import sys

import json



# 创建文件

# file_path:文件路径

# msg:即要写入的内容

def create_file(file_path, msg):

    with open(file_path, "wt", encoding="utf-8") as out_file:

        out_file.write(msg)

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

req = requests.get('https://geo.datav.aliyun.com/areas_v2/bound/100000_full.json')

req.encoding = 'utf-8'

result = json.loads(req.text)

# 写入文件

# with open('../geo_json/100000_全国_full.json', 'w') as file_obj:

#    json.dump(result, file_obj)

dict_area = dict()

dict_area['100000'] = '全国'

# 可写入带格式化后的json对象

create_file('./geo_json/100000_full.json', req.text)

# 读取并加载json文件 json.loads(操作的是字符串),json.load()操作的是文件流

with open('./geo_json/100000_full.json', 'r', encoding="utf-8") as file_obj:

    geo_json = json.load(file_obj)

    features = geo_json["features"]

    i = 0

    for province_feature in features:  # 循环各省

        adcode = str(province_feature["properties"]["adcode"])

        name = province_feature["properties"]["name"]

        dict_area[adcode] = name

        print('name{0} is {1}, adcode is {2}'.format(i, name, adcode))

        i = i + 1

        if adcode[4:] != '00':  # 直辖市的下一级别直接是区,没有市级别

            continue

        # 省份的url_json获取,获取省,及省内城市json

        province_full_url = 'https://geo.datav.aliyun.com/areas_v2/bound/{0}_full.json'.format(adcode)

        province_full_json = json.loads(requests.get(province_full_url).text)

        city_features = province_full_json["features"]

        j = 0

        for city_feature in city_features:  # 循环各市

            city_adcode = str(city_feature["properties"]["adcode"])

            city_name = city_feature["properties"]["name"]

            dict_area[city_adcode] = city_name

            print('name{0}-{1} is {2}, adcode is {3}'.format(i, j, city_name, city_adcode))

            #市的fulljson
            shiFull_json_url = 'https://geo.datav.aliyun.com/areas_v2/bound/{0}_full.json'.format(city_adcode)
            resp = requests.get(shiFull_json_url)
            if resp.status_code == 404:
                continue

            shiFull_json_ = json.loads(requests.get(shiFull_json_url).text)
            quxian_features= shiFull_json_["features"]
            for quxian_feature in quxian_features:  # 循环各市

                city_adcode = str(quxian_feature["properties"]["adcode"])

                city_name = quxian_feature["properties"]["name"]

                dict_area[city_adcode] = city_name

                print('name {0}, adcode is {1}'.format(city_name, city_adcode))
            j = j + 1

# 遍历字典

for area in dict_area.keys():

    print('{}:{}', area, dict_area[area])

    area_url = 'https://geo.datav.aliyun.com/areas_v2/bound/{0}.json'.format(area)

    req = requests.get(area_url)

    create_file('./geo_json/{0}.json'.format(area, dict_area[area]), req.text)

    area_full_url = 'https://geo.datav.aliyun.com/areas_v2/bound/{0}_full.json'.format(area)

    print(area_full_url)

    req = requests.get(area_full_url)

    if req.text.find("NoSuchKey") == -1:

        create_file('./geo_json/{0}_full.json'.format(area, dict_area[area]), req.text)

# json文件说明

# 中国json https://geo.datav.aliyun.com/areas_v2/bound/100000.json

# 中国+各省市json https://geo.datav.aliyun.com/areas_v2/bound/100000_full.json

# 山东省边界json   https://geo.datav.aliyun.com/areas_v2/bound/370000.json

# 山东省+省内地级市json   https://geo.datav.aliyun.com/areas_v2/bound/370000_full.json

# 德州市边界json https://geo.datav.aliyun.com/areas_v2/bound/371400.json

# 德州市+内部辖区json https://geo.datav.aliyun.com/areas_v2/bound/371400_full.json

# create_file('../geo_json/full.json', result)

# 读取并加载json文件```

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tutuxfsh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值