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