【发布时间】: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