python 全国行政地区信息爬取-腾迅位置服务平台

Api 接口:腾迅位置服务 / WebService API / 开发指南 / 行政区划

Key 注册:如何申请腾讯位置服务的密钥

python 代码

  • 获取省份、城市行政地理信息:名称、编码、拼音、经纬度
import json
import requests
import pandas as pd

def Get_ProvinceInfo(Key):
    '''  
        获取省份信息:名称、编码、拼音、经纬度
    '''
    url = "https://apis.map.qq.com/ws/district/v1/list"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
    }
    params = {
        "key":key
    }
    res = requests.get(url,params=params,headers=headers)
    res.encoding = "utf-8"
    js_data = json.loads(res.text)
    # 进一步处理
    province_df = pd.DataFrame(js_data['result'][0])
    province_df['pinyin'] = ["".join([j.capitalize() for j in i]) for i in province_df['pinyin']]
    province_df['lat'] = [i['lat'] for i in province_df['location']]
    province_df['lng'] = [i['lng'] for i in province_df['location']]
    province_df.drop(['location','cidx'],axis=1,inplace=True)
    
    return province_df

def Get_CityInfo(Key):
    '''  
        获取城市信息:名称、编码、拼音、经纬度
    '''
    url = "https://apis.map.qq.com/ws/district/v1/list"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
    }
    params = {
        "key":key
    }
    res = requests.get(url,params=params,headers=headers)
    res.encoding = "utf-8"
    js_data = json.loads(res.text)
    # 进一步处理
    city_df = pd.DataFrame(js_data['result'][1])
    city_df['pinyin'] = ["".join([j.capitalize() for j in i]) for i in city_df['pinyin']]
    city_df['lat'] = [i['lat'] for i in city_df['location']]
    city_df['lng'] = [i['lng'] for i in city_df['location']]
    city_df.drop(['location','cidx'],axis=1,inplace=True)
    city_df['province_id'] = [i[:2]+"0000" for i in city_df['id']]
    
    return city_df


if __name__ == "__main__":
    key = '你的Key'
    province = Get_ProvinceInfo(key)
    city = Get_CityInfo(key)
    
    province.to_excel("ProvinceInfo_TX.xlsx",index=False)
    city.to_excel("CityInfo_TX.xlsx",index=False)
  • province
    在这里插入图片描述
  • city
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值