0

Apify 的旧版 Crawler有一个randomWaitBetweenRequests选项:

此选项强制爬虫确保打开两个网页之间的最小时间间隔,以防止它使目标服务器过载。

Apify Actors 有类似的设置吗?如果是这样,它如何影响 Actor Units 计算?

4

1 回答 1

2

apify/web-scraper中没有类似的选项,它应该替换旧版爬虫选项。

但是有一种方法可以在 pageFunction 中自己实现。您可以简单地使用 context.waitFor() 函数并以毫秒为单位传递随机时间。

async function pageFunction(context) {
    const { request, log, jQuery } = context;

    // To be able to use jQuery as $, one needs save it into a variable
    // and select the inject jQuery option. We've selected it for you.
    const $ = jQuery;
    const title = $('title').text();

    log.info(`URL: ${request.url} TITLE: ${title}`);

    // This waits time in ms, which getRandomWait returns.
    await context.waitFor(getRandomWait());

    // To save data just return an object with the requested properties.
    return {
        url: request.url,
        title
    };
}

如果你想在 apify/web-scraper 中有这个选项,你可以在GitHub repo上提交一个问题。

于 2019-09-16T09:31:03.060 回答