爬取全国各省市县geo地图json文档的脚本(甘肃省各县为例)

python爬取全国各省市县geo地图json文档的脚本(甘肃省各县为例)

大哥要我做一个地图上下钻的功能,要求要下钻到县一级。然后我发现了一个可以得到省市json的网站(强推: http://datav.aliyun.com/tools/atlas/#&lat=30.37018632615852&lng=106.68898666525287&zoom=3.5
里面有在线的api(这下做起来容易了),大哥又告诉我最好把json放到本地文件。。。作为一个程序员手写是不可能手写的

上代码

import requests
import json

# 请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400'
}
# 甘肃省各县区划
cityList=[
620102,
620103,
620104,
620105,
620111,
620121,
620122,
620123,
620200,
620302,
620321,
620402,
620403,
620421,
620422,
620423,
620502,
620503,
620521,
620522,
620523,
620524,
620525,
620602,
620621,
620622,
620623,
620702,
620721,
620722,
620723,
620724,
620725,
620802,
620821,
620822,
620823,
620825,
620826,
620881,
620902,
620921,
620922,
620923,
620924,
620981,
620982,
621002,
621021,
621022,
621023,
621024,
621025,
621026,
621027,
621102,
621121,
621122,
621123,
621124,
621125,
621126,
621202,
621221,
621222,
621223,
621224,
621225,
621226,
621227,
621228,
622901,
622921,
622922,
622923,
622924,
622925,
622926,
622927,
623001,
623021,
623022,
623023,
623024,
623025,
623026,
623027]
def getJson(num):
    url = 'https://geo.datav.aliyun.com/areas_v2/bound/'+str(num)+'.json'

    response = requests.get(url, headers=headers)
    response.encoding = 'utf-8'
    # 读字符串转字典
    data = json.loads(response.text)
    # 这里是我做散点图需要用'cp'字段来定位 自行取舍 cp字段也确定地图各市县名的位置
    # data['features'][0]['properties']['cp']=data['features'][0]['properties'].pop("centroid")
    # print(data)
    with open('./甘肃省/'+str(data['features'][0]['properties']['adcode'])+".json","w",encoding="utf-8") as f:
        data = str(data)
        data = data.replace("'", '"')
        f.write(data)
    f.close()
    print(data)

for i in cityList:
    getJson(i)


结果

在这里插入图片描述

百度网盘

连接: https://pan.baidu.com/s/1iQJKAUHrBj6KdwznxobgyQ
提取码:6wg8

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值