如何通过分页从其他页面提取数据



我成功返回了数据的第一页,并且拥有API调用中存在的额外数据页的数量。

这是我试图提取额外数据页的代码。

try {
const response = UrlFetchApp.fetch(root + endpoint, params);
const responseCode = response.getResponseCode();
const returnedResults = response.getContentText();
const jsonResults = JSON.parse(returnedResults)
//console.log(responseCode) // *** return 200
//console.log(returnedResults) // *** return successful
//console.log(jsonResults) //*** return successful

Object.keys(jsonResults).forEach(function (key){
console.log(jsonResults[key]);
/*
{ count_per_page: 20,
total_count: 12261,
current_page: 1,
total_pages: 614,
_links: { next: { href: '/v2/organizations?page=2' } } }
*/
});
} catch (e) {
console.log('Error: ' + e);
}
const data = [];
let hasmoreitems = true;
while (hasmoreitems) {

hasmoreitems = jsonResults.pagination.current_page < jsonResults.pagination.total_pages;

data.forEach(entry => {
const name = entry['name'];
const email = entry['email'];
const mission = entry['mission_statement'];
const address = entry['address'].country;
data.push([
name, email, mission, address]);
});
// send data to spreadsheet
const sheet = SpreadsheetApp.openById('1fM-soJt8rJeIHYZ2vVKyoX9J-LohElqvGB2syhlRk00').getSheets()[1];
sheet.getRange(sheet.getLastRow()+1, 1, data.length,data[0].length).setValues(data);
}

分页的示例响应如下所示;

"pagination": {
"count_per_page": 20,
"total_count": 1,
"current_page": 1,
"total_pages": 2,
"_links": {
"next": {
"href": "/v2/organizations?page=2"
}
}
}

在检查了对象的结构后,我实现了键的不同级别。下面是工作代码。希望对你有帮助。

let all_data = [];
while (endpoint) {
let response = UrlFetchApp.fetch(root + endpoint, params);
let data = JSON.parse(response.getContentText());
let orgs = data.organizations
Object.keys(data).forEach(function (key) {
console.log(data[key]);
});
endpoint = data.pagination._links.next.href;
orgs.forEach((entry) => {
all_data.push([
entry.name,
entry.email,
entry.mission_statement,
entry.address.country,
]);
//console.log(all_data)
});

}

不知道你的实际反应是什么样子是很困难的,但我认为这会做你想要的:

let endpoint = "/v2/organizations?page=1";
let all_data = [];
while (endpoint) {
let response = UrlFetchApp.fetch(root + endpoint, params);
let data = JSON.parse(response.getContentText());
endpoint = data._links.next.href;
data.forEach((entry) => {
all_data.push([
entry.name,
entry.email,
entry.mission_statement,
entry.address.country,
]);
});
}

最新更新