【问题标题】:How to combine all pages from API into one JSON response with python?如何使用 python 将 API 中的所有页面组合成一个 JSON 响应?
【发布时间】:2021-06-19 14:29:30
【问题描述】:

我一直在尝试创建一个循环,遍历 API 响应的所有页面并将它们组合成一个输出,最好是一个数据帧。我用他们提供的测试信息替换了所有 API 信息。

首先我用这个设置我的基本导入等:

# Dependencies and Setup
import pandas as pd
import requests
import os
import json

# Create a cleaned json response
def jprint(obj):
    text = json.dumps(obj, sort_keys=True, indent=4)
    print(text)

# Define variables
access_token = "xxx"
base_url = "https://api.bridgedataoutput.com/api/v2/OData"
endpoint = "/test/Property/replication"
url = base_url+endpoint
headers = {'Authorization': 'Bearer '+access_token}

# Set query parameters (I limit this to 10 records just to make sure the request works)
payload={
  '$filter': "PropertyType eq 'Residential' and StandardStatus eq 'Closed' and PropertySubType ne 'Single Family Residence' and PropertySubType ne 'Townhouse'",
  '$top': 10
    }

# Make get request
response = requests.request("GET", url, headers=headers, data=payload)

# Print first page of results
jprint(response.json())

我在响应的正文中得到了这个:

{
@odata.context: "https://api.bridgedataoutput.com/api/v2/OData/test/$metadata#Property",
@odata.nextLink: "https://api.bridgedataoutput.com/api/v2/OData/test/Property/replication?$next=1587765844387b617316828687dcd8f60d9e7d72efcc1",
@odata.count: 6550,
value: [
{...},
{...},
{...},
{...},
{...},
{...},
{...},
{...},
{...},
{...}
]

@odata.nextLink 网址将我带到结果的第二页,除了将 $next='xxx' 更改为另一组看似随机的数字和字母外,@odata.nextLink 网址相同。

【问题讨论】:

  • 老兄不要在帖子中添加令牌
  • 这是一个垃圾令牌。每个人都可以访问测试集

标签: python pagination get


【解决方案1】:

您可以实现一个while 循环,直到返回的json 中不再有@odata.nextLink 为止。您可以使用['value'] 中的所有数据扩展一个列表,在这种情况下,这将产生一个包含 27 个项目的列表:

# Dependencies and Setup
import pandas as pd
import requests
import os
import json

# Create a cleaned json response
def jprint(obj):
    text = json.dumps(obj, sort_keys=True, indent=4)
    print(text)

# Define variables
access_token = "6baca547742c6f96a6ff71b138424f21"
base_url = "https://api.bridgedataoutput.com/api/v2/OData"
endpoint = "/test/Property/replication"
url = base_url+endpoint
headers = {'Authorization': 'Bearer '+access_token}

# Set query parameters (I limit this to 10 records just to make sure the request works)
payload={
  '$filter': "PropertyType eq 'Residential' and StandardStatus eq 'Closed' and PropertySubType ne 'Single Family Residence' and PropertySubType ne 'Townhouse'",
  '$top': 10
    }

# Make get request
response = requests.request("GET", url, headers=headers, data=payload)
response_data = response.json()

#create list to store the values
data = []
data.extend(response_data['value'])

while True:
  response = requests.request("GET", response_data['@odata.nextLink'], headers=headers, data=payload)
  response_data = response.json()
  if '@odata.nextLink' in response_data.keys():
    data.extend(response_data['value'])
  else:
    break

【讨论】:

  • 你知道我如何将它保存到没有所有括号等的熊猫数据框中吗?我成功地将它放入 csv 文件,但我想去掉括号和其他 json 格式。
  • df = pd.DataFrame(data)。然后就可以导出到csvdf.to_csv('filename.csv)
猜你喜欢
  • 1970-01-01
  • 2021-05-04
  • 2021-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
相关资源
最近更新 更多