Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

1.2 KB
Raw Blame
import { Actor } from 'apify';
import { CheerioCrawler, createCheerioRouter, Dataset } from '@crawlee/cheerio';

const mainOptions = {
    exit: Actor.isAtHome(),
    storage:
        process.env.STORAGE_IMPLEMENTATION === 'LOCAL'
            ? new (await import('@apify/storage-local')).ApifyStorageLocal()
            : undefined,
};

const router = createCheerioRouter();

router.addHandler('START', async ({ enqueueLinks }) => {
    await enqueueLinks({
        label: 'DETAIL',
        include: ['**/examples/accept-user-input'],
    });
});

router.addHandler('DETAIL', async ({ request, $ }) => {
    const { url } = request;

    const uniqueIdentifier = url.split('/').slice(-2).join('/');
    const title = $('.markdown h1').text();
    const firstParagraph = $('.markdown > p').text();
    const modifiedDate = $('.theme-last-updated time').attr('datetime');

    await Dataset.pushData({
        url,
        uniqueIdentifier,
        title,
        firstParagraph,
        modifiedDate,
    });
});

await Actor.main(async () => {
    const crawler = new CheerioCrawler({
        requestHandler: router,
    });

    await crawler.run([{ url: 'https://crawlee.dev/js/docs/3.12/examples', userData: { label: 'START' } }]);
}, mainOptions);