我为一个网站写了一个半工作的抓取脚本:
async function pageFunction(context) {
const {
request,
log,
skipLinks,
jQuery: $,
waitFor
} = context;
log.info('Pagination');
let timeoutMillis; // undefined
const buttonSelector = 'div.pagination-view-more';
//click on Show more button 5 times
for (let step = 0; step < 5; step++) {
log.info('Waiting for the "Show more" button.');
try {
await waitFor(buttonSelector, {
timeoutMillis
}); // Default timeout first time.
timeoutMillis = 5000; // 2 sec timeout after the first.
} catch (err) {
// Ignore the timeout error.
log.info('Could not find the "Show more button", we\'ve reached the end.');
break;
}
log.info('Clicking the "Show more" button.');
$(buttonSelector).click();
}
//export the results
var result = [];
$(".thing-card").each(function() {
result.push({
title: $(this).attr('title'),
//format Dec 15, 2019
date: $(this).find('.item-header .item-date').text().replace(/\s/g, ''),
});
});
return result;
}
在上面的示例中,我在“显示更多”按钮上单击了 5 次,并尝试导出标题和日期作为结果。问题是,我没有得到所有的结果,我认为脚本完成得比它应该的要早。
在最终脚本中,我想删除固定的 for 循环并运行此循环,直到结果的日期从今天起最多 -7 天(或 1 周)。Apify 有可能吗?