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.8 KB
Raw Blame
import { Actor } from 'apify';
import { Dataset, PlaywrightCrawler } from '@crawlee/playwright';

const expectedCookies = [
    {
        name: 'initial_request',
        value: 'true',
    },
    {
        name: 'session',
        value: 'true',
    },
    {
        name: 'hook_request',
        value: 'true',
    },
];

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

await Actor.main(async () => {
    const crawler = new PlaywrightCrawler({
        preNavigationHooks: [
            async ({ session, request, gotoOptions }) => {
                await session.setCookie('session=true', request.url);
                request.headers.cookie = 'hook_request=true';

                gotoOptions.waitUntil = 'networkidle';
            },
        ],
        async requestHandler({ page }) {
            const initialCookiesLength = expectedCookies.length;

            const pageCookies = await page.context().cookies();

            let numberOfMatchingCookies = 0;
            for (const cookie of expectedCookies) {
                if (
                    pageCookies.some(
                        (pageCookie) => pageCookie.name === cookie.name && pageCookie.value === cookie.value,
                    )
                ) {
                    numberOfMatchingCookies++;
                }
            }

            await Dataset.pushData({ initialCookiesLength, numberOfMatchingCookies });
        },
    });

    await crawler.run([
        {
            url: 'https://api.apify.com/v2/browser-info',
            headers: {
                Cookie: 'initial_request=true',
            },
        },
    ]);
}, mainOptions);