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.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
import { once } from 'node:events';
import { URL } from 'node:url';
import { Worker, workerData } from 'node:worker_threads';
import { CheerioCrawler, Dataset, serviceLocator } from '@crawlee/cheerio';
import { Actor } from 'apify';
process.env.CRAWLEE_PURGE_ON_START = '0';
const mainOptions = {
exit: Actor.isAtHome(),
storage:
process.env.STORAGE_IMPLEMENTATION === 'LOCAL'
? new (await import('@apify/storage-local')).ApifyStorageLocal()
: undefined,
};
const thisFile = new URL(import.meta.url);
if (workerData !== '#actor') {
const firstRun = new Worker(thisFile, {
workerData: '#actor',
});
const [firstExitCode] = await once(firstRun, 'exit');
const secondRun = new Worker(thisFile, {
workerData: '#actor',
});
const [secondExitCode] = await once(secondRun, 'exit');
if (firstExitCode !== 0 || secondExitCode !== 0) {
throw new Error(`Unexpected exit code:\nfirst run: ${firstExitCode}\nsecond run: ${secondExitCode}`);
}
} else {
await Actor.main(async () => {
const crawler = new CheerioCrawler({
maxConcurrency: 1,
maxRequestsPerCrawl: 5,
async requestHandler({ enqueueLinks, request }) {
const { url } = request;
await enqueueLinks();
await Dataset.pushData({ url });
process.emit('SIGINT');
},
});
serviceLocator.getStorageBackend().__purged = false;
await crawler.run(['https://crawlee.dev']);
}, mainOptions);
}