我使用PowerShell从供应商站点获取位置列表,使用API调用:
# Set the initial page and limit values
$page = 1
$limit = 1000
# Initialize an array to hold all the data
$allData = @()
# Set the headers with the token key
$headers = @{
'Content-Type' = 'application/json'
'Authorization' = 'Bearer SomeTokenKey'
'Accept'= 'application/json'
}
# Loop until all data is retrieved
while ($true) {
# Set the API endpoint URL with the current page and limit values
$uri = "https://mysite.Someapisite.com/api/locations?page=$page&limit=$limit"
# Make the API call and retrieve the data
$responseData = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers -Body $body
# Add the retrieved data to the allData array
$allData += $responseData.data
# Check if all data has been retrieved
if ($responseData.meta.pagination.current_page -eq $responseData.meta.pagination.total_pages) {
break
}
# Set the page and limit values for the next API call
$page = $responseData.meta.pagination.current_page + 1
$limit = $responseData.meta.pagination.per_page
}
# Export all the data to a CSV file
$allData | Export-Csv -Path 'c:\extracts\locations.csv' -NoTypeInformation -Force
供应商限制每次呼叫最多提取1000条记录。供应商API文档说明支持分页:
该站点通过限制和页面参数利用基于偏移/限制的分页,其中偏移是基于所提供的参数自动计算的。
Link: <https://mySite.someAPIsite.com/api/location?page=3&limit=10>; rel="next",
<https://mySite.someAPIsite.com/api/location?page=7&limit=10>; rel="last"
我的目标是提取源代码中的所有记录,但上面的脚本只返回1000条记录。
(A)难道我没有正确地实现这一点吗?
(B)使用分页是否会绕过记录限制?
有什么想法吗
1条答案
按热度按时间6ojccjat1#
我对代码进行了修改,并使其满足了我的需要: