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 { Actor } from 'apify';
import { CheerioCrawler, ConcurrencySystem, Dataset } from '@crawlee/cheerio';
const mainOptions = {
exit: Actor.isAtHome(),
storage:
process.env.STORAGE_IMPLEMENTATION === 'LOCAL'
? new (await import('@apify/storage-local')).ApifyStorageLocal()
: undefined,
};
await Actor.main(async () => {
// The caller owns an injected system's lifecycle - start it before the crawler runs and stop it afterwards.
const concurrencySystem = new ConcurrencySystem({ desiredConcurrency: 2 });
const crawler = new CheerioCrawler({
maxRequestsPerCrawl: 10,
concurrencySystem,
async requestHandler({ $, request }) {
const {
url,
userData: { label },
} = request;
if (label === 'START') {
const links = $('a.card')
.toArray()
.map((item) => $(item).attr('href'));
for (const link of links) {
const actorDetailUrl = `https://crawlee.dev${link}`;
await crawler.addRequests([
{
url: actorDetailUrl,
userData: { label: 'DETAIL' },
},
]);
}
} else if (label === 'DETAIL') {
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 concurrencySystem.start();
try {
await crawler.run([{ url: 'https://crawlee.dev/js/docs/examples', userData: { label: 'START' } }]);
} finally {
await concurrencySystem.stop();
}
}, mainOptions);