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.

814 B
Raw Blame
import { AdaptivePlaywrightCrawler, adaptivePlaywrightCrawlerStatisticState, Statistics } from 'crawlee';
import { z } from 'zod';

const statistics = new Statistics({
    stateExtension: {
        // The adaptive crawler tracks fields of its own, so an injected instance has to carry them too.
        deserialize: adaptivePlaywrightCrawlerStatisticState.deserialize.extend({
            productsFound: z.number().default(0),
        }),
    },
});

const crawler = new AdaptivePlaywrightCrawler({
    statistics,
    async requestHandler({ querySelectorAll }) {
        statistics.state.productsFound += (await querySelectorAll('.product')).length;
    },
});

await crawler.run(['https://crawlee.dev']);

console.log(`Handled ${crawler.statistics.state.httpOnlyRequestHandlerRuns} requests without a browser`);