【发布时间】:2021-08-07 17:07:38
【问题描述】:
我有一个 API,其中所有位置 ID 及其各自的信息(如(地址、纬度、经度)都存在。但是,如果我想获取其他额外属性,例如位置名称、位置区域、位置访问权限,那么我需要在 API 中将位置 id 作为参数一一提供,以获取它们各自的额外属性。
我已经写了下面的代码。但是下面代码的问题是数据进入控制台,我不知道如何在 json 中获取这些信息,然后将其转换为文本文件。
ids=location_id_df["id"] #stored location id in dataframe
authorization =”####################### "
print("started")
def test_api(url, authorization, rawfile,ids):
for i in range(0,1000,50):
for j in ids:
#print(j)
try:
request = urllib.request.Request('https:….. /locations/{}'.format(j)+"?
offset="+str(i),headers={'authorization':authorization})
response = urllib.request.urlopen(request).read()
print(response)
except HTTPError as e:
print(e)
sys.exit(0)
with open(rawfile + "_offset_" + str(i) + ".json", "wb") as json_download:
json_download.write(response)
test_api(url, authorization, rawfile,ids)
我需要像 json 一样获取响应
5182021_offset_0.json #contains some location id's with extra attribute data
5182021_offset_50.json #contains some location id's with extra attribute data
5182021_offset_100.json #contains some location id's with extra attribute data
........................
.......................
【问题讨论】:
标签: python arrays json dataframe api