我正在构建一个应用程序,它在Airtable上使用Requests库获取数据库,并将其转换为列表。
我的问题是,我只能通过URL请求获得100条记录。我试图在Airtable API中找到解决方案,但是更改URL以插入maxRecords并不起作用。
我使用这个URL来获取数据。但它只返回100条记录。
https://api.airtable.com/v0/appP6G7OJmLzUCoUt/Table%201?api_key=MY_KEY我读过有关maxRecords和分页的文章,但我找不到一种方法来更改URL来使用它。
有人能帮我吗?
发布于 2018-02-28 12:13:56
正如我在其他帖子中看到的,许多人都在处理同样的问题。我试着找到solucions,但我不能用URL修复它。
不过,谢天谢地,使用Airtable API库在Python中从Airtable获取所有数据要容易得多。(http://airtable-python-wrapper.readthedocs.io/en/master/)
有一个名为get_all()的函数,它接受maxRecords参数。只需调用x.get_all(),内部不带参数,API将返回表中的每条记录。
发布于 2019-05-17 09:50:51
TLDR:
您可以尝试使用当http响应(json)中存在偏移量时执行的递归函数。
Acknowledgment:
这个解决方案花了7个小时的研究、故障排除和来自伟大而强大的Doug的建议。该项目使用Alamofire执行http请求,并使用SwiftyJson访问JSON。
原因:
在Airtable文档中,他们声明他们的速率限制为每个请求100个项目。如果请求超过100个条目,则请求中会包含偏移量。
它们会给出一个指令,将偏移量包含在你的下一个请求中,以获得下一个100个结果。但是,他们没有解释或演示如何做到这一点。
解决方案:
它已经过测试,可以从25个http请求中检索2565个项目。用Swift编写,逻辑很简单:
编写一个带有可选偏移量参数的递归函数。调用不带偏移量的函数。检查http响应(json)中的偏移量。如果存在偏移量,则将http请求(json)存储在函数外部的数组中。然后,从内部调用相同的函数--这次使用偏移量。
这里是扩展的code。
func requestAirtableRecords(forTable table: String, withTableView tableView: String, withOffset offset: String?, completion: @escaping ([JSON]) -> ()) {
let parameters: [String: Any] = offset != nil ? ["view": tableView, "offset": offset!] : ["view": tableView]
do {
let url: URLRequest = try self.requestRecordsURL(table: table, method: HttpRequest.get, parameters: parameters)!
Alamofire.request(url).responseJSON { (response) in
switch response.result {
case .success(_):
let json = JSON(response.result.value!)
self.jsonArray.append(json)
let nextOffset = json["offset"]
if nextOffset.exists() {
self.requestAirtableRecords(forTable: table, withTableView: tableView, withOffset: nextOffset.stringValue, completion: { _ in
completion(self.jsonArray)
})
} else {
completion(self.jsonArray)
}
case .failure(let error):
print(error)
}
}
} catch {
print("Error: Unable to request records from Airtable.")
}
}发布于 2020-03-09 03:37:59
这是我在Python中用来解决这个问题的代码-与Christian的答案一致,我使用了递归调用。然而,我发布了这段代码b/c,OP要求用Python回答。
def get_airtable_data(email_dict=None, offset=None):
"""
queries airtable and creates a dict of {name: email} from records that are
listed as subscribed
"""
if email_dict is None:
email_dict = {}
r = requests.get(request_url, headers={"Authorization": bearer_token}, params={"pageSize": 100, "offset": offset})
r_json = json.loads(r.text)
for item in r_json["records"]:
if item["fields"]["subscribed"] == 1:
email_dict[item["fields"]["Name"]] = item["fields"]["Primary Email"]
if r_json["offset"] is None:
print("all data loaded")
else:
try:
get_airtable_data(email_dict, r_json["offset"])
except KeyError:
print("all data loaded")
return email_dicthttps://stackoverflow.com/questions/48998254
复制相似问题