In this paper, the latest data information (province, city, district, latitude and longitude, administrative level, city code, administrative code, etc.) is obtained by fetching the Gaode administrative division query interface and deposited into the mysql database through the
The table structure is designed as follows:
CREATE TABLE `districts` ( `districtId` int(11) NOT NULL AUTO_INCREMENT, `districtPid` int(11) DEFAULT NULL COMMENT 'Superior ID', `name` varchar(32) DEFAULT NULL COMMENT 'Name of the administrative district', `citycode` varchar(6) DEFAULT NULL COMMENT 'City code', `adcode` varchar(6) DEFAULT NULL COMMENT 'Urban area code', `lng` float(13,10) DEFAULT NULL COMMENT 'Longitude', `lat` float(13,10) DEFAULT NULL COMMENT 'Latitude', `level` varchar(10) DEFAULT NULL COMMENT 'Administrative division level', `createTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `updateTime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`districtId`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4
The python code is as follows: (you need to replace the account password with your own database address, and replace it with your own high-degree key value)
# coding = utf-8 """ @autor: linuxdba """ import json import import requests header = { 'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Mobile Safari/537.36' } mydb = (host='ip.....', user='username.....', password='pwd.....', port=3306, database='test') mycursor = () insert_sql = "INSERT INTO `districts` ( `districtpid`,`name`, `adcode`, `lng`, `lat`, `level`) VALUES (%s,%s, %s, %s, %s, %s);" insert_city_sql = "INSERT INTO `districts` ( `districtpid`,`name`, `citycode`,`adcode`, `lng`, `lat`, `level`) VALUES (%s, %s, %s, %s, %s, %s, %s);" select_sql = 'select districtId from districts where adcode = %s' def updatedb(sql, *args): try: (sql, args) () except Exception as e: print(e) def selectdb(sql, *args): (sql, args) return mycursor updatedb(insert_sql, 0, 'People's *', '100000', '116.3683244', '39.915085', 'country') provinces = ['Beijing Municipality', 'Tianjin', 'Hebei Province', 'Shanxi Province', 'Inner * Autonomous Region', 'Liaoning Province', 'Jilin Province', 'Heilongjiang Province', 'Shanghai', 'Jiangsu Province', 'Zhejiang Province', 'Anhui Province', 'Fujian Province', 'Jiangxi Province', 'Shandong Province', 'Henan Province', 'Hubei Province', 'Hunan Province', 'Guangdong Province', 'Guangxi Zhuang Autonomous Region', 'Hainan Province', 'Chongqing Municipality', 'Sichuan Province', 'Guizhou Province', 'Yunnan Province', 'Tibet Autonomous Region', 'Shaanxi Province', 'Gansu Province', 'Qinghai Province', 'Ningxia Hui Autonomous Region', '* Uygur Autonomous Region', '* Province', '* Special Administrative Region', 'Macao Special Administrative Region'] for i in provinces: code_url = '/v3/config/district?key=d6efa016ea883e15a0782f939d6a805e&keywords={}&subdistrict=2'.format( i) res = (code_url, headers=header) print(code_url) print() province = ()['districts'] adcode = province[0]['adcode'] pname = province[0]['name'] center = province[0]['center'] pcitycode = province[0]['citycode'] level = province[0]['level'] lng = province[0]['center'].split(',')[0] lat = province[0]['center'].split(',')[1] city_list = province[0]['districts'] # print(pname, pcitycode, adcode, lng, lat, level) updatedb(insert_sql, 1, pname, adcode, lng, lat, level) districtpid = selectdb(select_sql, adcode).fetchone()[0] # print(city_list) for city in city_list: citycode = city['citycode'] adcode = city['adcode'] name = city['name'] level = city['level'] lng = city['center'].split(',')[0] lat = city['center'].split(',')[1] district_list = city['districts'] # print(name, citycode, adcode, lng, lat, level) updatedb(insert_city_sql, districtpid, name, citycode, adcode, lng, lat, level) citypid = selectdb(select_sql, adcode).fetchone()[0] for district in district_list: citycode = district['citycode'] adcode = district['adcode'] name = district['name'] level = district['level'] lng = district['center'].split(',')[0] lat = district['center'].split(',')[1] district_list = district['districts'] if level in ['district']: updatedb(insert_city_sql, citypid, name, citycode, adcode, lng, lat, level) print(name, citycode, adcode, lng, lat, level)
The results are as follows:
Table data file link:
/files/ orClick herelocal download
summarize
This article on python to get the latest national provincial and municipal data and stored in the table of the article is introduced to this, more related python to get the provincial and municipal data content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!