Actor picture

Example Process Crawl Results

apify/example-process-crawl-results

Iterates through all results from a crawler run and count them. Needs to be called from the crawler's finish webhook by adding an URL to finish the webhook of your crawler. Use this actor as a starting point to develop custom post-processing of data from the crawler.

No credit card required

Author's avatarApify Technologies
  • Modified
  • Users15
  • Runs12,544
Actor picture

Example Process Crawl Results

Based on the apify/actor-node-basic:v0.21.10 Docker image (see docs).

const Apify = require('apify');
const _ = require('underscore');

Apify.main(async () => {
    // Get act input and validate it
    const input = await Apify.getValue('INPUT');
    console.log('Input:')
    console.dir(input);
    if (!input || !input._id) {
        throw new Error('Input is missing the "_id" attribute. Did you start it from crawler finish webhook?');
    }
    const executionId = input._id;
    
    // Print info about crawler run
    const crawlerRunDetails = await Apify.client.crawlers.getExecutionDetails({ executionId });
    if (!crawlerRunDetails) {
        throw new Error(`There is no crawler run with ID: "${executionId}"`);
    }
    console.log(`Details of the crawler run (ID: ${executionId}):`);
    console.dir(crawlerRunDetails);
    
    // Iterate through all crawler results and count them
    // Here is the place where you can add something more adventurous :)
    console.log(`Counting results from crawler run...`);
    
    const limit = 100;
    let offset = 0;
    let totalItems = 0;
    let results;
    
    do {
        results = await Apify.client.crawlers.getExecutionResults({ 
            executionId,
            limit,
            offset
        });
        
        offset += results.count;
        totalItems += results.items.length;
    } while (results.count > 0);
    
    // Save results
    console.log(`Found ${totalItems} records`);
    await Apify.setValue('OUTPUT', {
        crawlerRunDetails,
        totalItems
    });
    
});