# -*- coding: utf-8 -*-
import openpyxl as op
import urllib.request # 发送请求
from urllib import parse # URL编码
import json # 解析json数据
from math import ceil
from openpyxl import load_workbook # 从Excel中读取镇街名称
# 关键字搜索API服务地址:https://restapi.amap.com/v3/place/text?parameters/ 请求方式 GET
# parameters代表的参数包括必填参数和可选参数。所有参数均使用和号字符(&)进行分隔。
mykey = "替换为自己的key"
city = "四川"
keywords = "物资库"
types = ""
dict_location = {}
def get_page():
parameters = "key={}&keywords={}&types={}&city={}&children=0&offset=&page=1&extensions=all" \
.format(mykey, keywords, types, city) # 参数
url = "https://restapi.amap.com/v3/place/text?{}".format(parameters) # 拼接请求
newUrl = parse.quote(url, safe="/:=&?#+!$,;'@()*[]") # 编码
response = urllib.request.urlopen(newUrl) # 发送请求
data = response.read() # 接收数据
jsonData = json.loads(data) # 解析json文件
page = eval(jsonData['count'])
return page
def get_json(page):
parameters = "key={}&keywords={}&types={}&city={}&children=0&offset=&page={}&extensions=all" \
.format(mykey, keywords, types, city, page) # 参数
url = "https://restapi.amap.com/v3/place/text?{}".format(parameters) # 拼接请求
newUrl = parse.quote(url, safe="/:=&?#+!$,;'@()*[]") # 编码
response = urllib.request.urlopen(newUrl) # 发送请求
data = response.read() # 接收数据
jsonData = json.loads(data) # 解析json文件
return jsonData
# 写入数据入字典
page = ceil(get_page() / 20)
data = []
for i in range(1, page + 1):
jsonData = get_json(i)
for i in range(len(jsonData['pois'])):
name = jsonData['pois'][i]['name']
dict_location = {name: jsonData['pois'][i]['location']}
data.append(dict_location)
def op_toExcel(data, fileName): # openpyxl库储存数据到excel
wb = op.Workbook() # 创建工作簿对象
ws = wb['Sheet'] # 创建子表
ws.append(['name', 'position1', 'position2']) # 添加表头
for i in range(len(data)):
for key, value in data[i].items():
value = value.split(',')
d = key, value[0], value[1]
ws.append(d) # 每次写入一行
wb.save(fileName)
fileName = 'position.xlsx'
op_toExcel(data, fileName)
'''
# 读取
from openpyxl import load_workbook
wb2 = load_workbook('position.xlsx')
'''
修改了一下,把key和查询信息封装再来一个类里,然后通过一个main函数直接得到查询结果
# -*- coding: utf-8 -*-
import json # 解析json数据
import urllib.request # 发送请求
from math import ceil
from urllib import parse # URL编码
import openpyxl as op
# 关键字搜索API服务地址:https://restapi.amap.com/v3/place/text?parameters/ 请求方式 GET
# parameters代表的参数包括必填参数和可选参数。所有参数均使用和号字符(&)进行分隔。
class Info:
def __init__(self, mykey, city, keywords):
self.mykey = mykey
self.city = city
self.keywords = keywords
self.types = ""
# mykey = "22709a08ff193cc3493ebbdae106c1a3"
# city = "四川"
# keywords = "物资库"
# types = ""
# dict_location = {}
def main(input, fileName):
dict_location = {}
def get_page():
parameters = "key={}&keywords={}&types={}&city={}&children=0&offset=&page=1&extensions=all" \
.format(input.mykey, input.keywords, input.types, input.city) # 参数
url = "https://restapi.amap.com/v3/place/text?{}".format(parameters) # 拼接请求
newUrl = parse.quote(url, safe="/:=&?#+!$,;'@()*[]") # 编码
response = urllib.request.urlopen(newUrl) # 发送请求
data = response.read() # 接收数据
jsonData = json.loads(data) # 解析json文件
page = eval(jsonData['count'])
return page
def get_json(page):
parameters = "key={}&keywords={}&types={}&city={}&children=0&offset=&page={}&extensions=all" \
.format(input.mykey, input.keywords, input.types, input.city, page) # 参数
url = "https://restapi.amap.com/v3/place/text?{}".format(parameters) # 拼接请求
newUrl = parse.quote(url, safe="/:=&?#+!$,;'@()*[]") # 编码
response = urllib.request.urlopen(newUrl) # 发送请求
data = response.read() # 接收数据
jsonData = json.loads(data) # 解析json文件
return jsonData
# 写入数据入字典
page = ceil(get_page() / 20)
data = []
for i in range(1, page + 1):
jsonData = get_json(i)
for i in range(len(jsonData['pois'])):
name = jsonData['pois'][i]['name']
dict_location = {name: jsonData['pois'][i]['location']}
data.append(dict_location)
def op_toExcel(data, fileName): # openpyxl库储存数据到excel
wb = op.Workbook() # 创建工作簿对象
ws = wb['Sheet'] # 创建子表
ws.append(['name', 'position1', 'position2']) # 添加表头
for i in range(len(data)):
for key, value in data[i].items():
value = value.split(',')
d = key, value[0], value[1]
ws.append(d) # 每次写入一行
wb.save(fileName)
fileName = fileName + '.xlsx'
op_toExcel(data, fileName)
# 测试main,物资库的查询
test_info = Info("22709a08ff193cc3493ebbdae106c1a3", "四川", "物资库")
main(test_info, 'position')
# 查询民政局
info2 = Info("22709a08ff193cc3493ebbdae106c1a3", "四川", "民政局")
main(info2, 'mingzhengju')
'''
# 读取
from openpyxl import load_workbook
wb2 = load_workbook('position.xlsx')
'''
最终输出结果如下图所示