## 通过高德提供的开发者平台提供的行政区域划分接口获取2级行政区,接口链接可以到高德开发者平台拿到
代码部分,程序运行成功后会在同级目录生成一个文件,并且会保存到数据库
import json
import pymysql
import requests
class GaoDeSpider:
def __init__(self):
self.name = ['北京市', '上海市', "天津市", "重庆市", "黑龙江省", "吉林省", "辽宁省", "内蒙古自治区", "河北省",
"山西省", "陕西省", "山东省", "新疆维吾尔自治区", "青海省", "甘肃省", "宁夏回族自治区", "河南省",
"江苏省", "湖北省", "浙江省", "安徽省", "福建省", "江西省", "湖南省", "贵州省", "四川省", "广东省",
"云南省", "广西壮族自治区", "海南省", "台湾", "西藏自治区", "香港", "澳门"]
self.url = "https://restapi.amap.com/v3/config/district?keywords={}"
self.params = {
'subdistrict': '2', # 表示获取下两级行政区域(省/市/区)
'key': '高德注册应用的key',
}
self.db = pymysql.connect(host='localhost', user='用户', password='密码', database='数据库', charset='utf8') # 连接数据库
self.cur = self.db.cursor() # 创建游标
def save_2_mysql(self, tradename, id):
‘’‘保存到数据库’‘’
insert_sql = 'insert into citys (name, parent_id) values (%s, %s)'
self.cur.execute(insert_sql, [tradename, id])
self.db.commit()
def district(self):
‘’‘获取每个省的下两级行政区域数据’‘’
districts = []
for name in self.name:
response = requests.get(self.url.format(name), params=self.params)
datas = json.loads(response.text)
if datas['status'] == '1':
for data in datas['districts']:
province = {}
province['name'] = data['name'] # 名称
province['code'] = data['adcode'] # 区域编码
province['districts'] = []
self.save_2_mysql(province['name'], 0)
for every in data['districts']:
city = {}
city['name'] = every['name']
city['code'] = every['adcode']
city['districts'] = []
self.save_2_mysql(city['name'], 1)
for i in every['districts']:
district = {}
district['name'] = i['name']
district['code'] = i['adcode']
city['districts'].append(district)
self.save_2_mysql(district['name'], 2)
province['districts'].append(city)
districts.append(province) # 构建json格式的数据
with open('gaodecitys.json', 'w') as f:
f.write(json.dumps(districts)) # 保存json数据
self.cur.close()
self.db.close() # 关闭数据库连接
if __name__ == '__main__':
app = GaoDeSpider()
app.district()
网友评论