apify
TypeScript icon, indicating that this package has built-in type declarations

3.2.6 • Public • Published

Apify SDK

npm version Downloads Chat on discord Build Status

Apify SDK provides the tools required to run your own Apify Actors. The crawlers and scraping related tools, previously included in Apify SDK (v2), have been split into a brand-new module - crawlee, while keeping the Apify specific parts in this module.

Would you like to work with us on Crawlee, Apify SDK or similar projects? We are hiring Node.js engineers.

Upgrading from v2

A lot of things have changed since version 2 of the Apify SDK, including the split of the crawlers to the new crawlee module. We've written a guide to help you easily migrate from v2 to v3. Visit the Upgrading Guide to find out what changes you need to make (especially the section related to this very Apify SDK), and, if you encounter any issues, join our Discord server for help!

Quick Start

This short tutorial will set you up to start using Apify SDK in a minute or two. If you want to learn more, proceed to the Apify Platform guide that will take you step by step through running your Actor on Apify's platform.

Apify SDK requires Node.js 16 or later. Add Apify SDK to any Node.js project by running:

npm install apify crawlee playwright

For this example, we'll also install the crawlee module, as it now provides the crawlers that were previously exported by Apify SDK. If you don't plan to use crawlers in your Actors, then you don't need to install it. Keep in mind that neither playwright nor puppeteer are bundled with crawlee in order to reduce install size and allow greater flexibility. That's why we manually install it with NPM. You can choose one, both, or neither.

There are two ways to initialize your Actor: by using the Actor.main() function you're probably used to, or by calling Actor.init() and Actor.exit() manually. We prefer explicitly calling init and exit.

Using Actor.init() and Actor.exit()

import { Actor } from 'apify';
import { PlaywrightCrawler } from 'crawlee';

await Actor.init()

const crawler = new PlaywrightCrawler({
    async requestHandler({ request, page, enqueueLinks }) {
        // Extract HTML title of the page.
        const title = await page.title();
        console.log(`Title of ${request.url}: ${title}`);

        // Add URLs that point to the same hostname.
        await enqueueLinks();
    },
});

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

await Actor.exit();

Using Actor.main()

import { Actor } from 'apify';
import { PlaywrightCrawler } from 'crawlee';

await Actor.main(async () => {
    const crawler = new PlaywrightCrawler({
        async requestHandler({ request, page, enqueueLinks }) {
            // Extract HTML title of the page.
            const title = await page.title();
            console.log(`Title of ${request.url}: ${title}`);

            // Add URLs that point to the same hostname.
            await enqueueLinks();
        },
    });

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

Support

If you find any bug or issue with the Apify SDK, please submit an issue on GitHub. For questions, you can ask on Stack Overflow or contact support@apify.com

Contributing

Your code contributions are welcome, and you'll be praised to eternity! If you have any ideas for improvements, either submit an issue or create a pull request. For contribution guidelines and the code of conduct, see CONTRIBUTING.md.

License

This project is licensed under the Apache License 2.0 - see the LICENSE.md file for details.

Acknowledgments

Many thanks to Chema Balsas for giving up the apify package name on NPM and renaming his project to jsdocify.

Package Sidebar

Install

npm i apify

Weekly Downloads

10,843

Version

3.2.6

License

Apache-2.0

Unpacked Size

357 kB

Total Files

33

Last publish

Collaborators

  • apify-service-account
  • mtrunkat
  • jancurn
  • petrpatek
  • mnmkng
  • jaroslavhejlek
  • drobnikj
  • metalwarrior665
  • fnesveda
  • b4nan