The Apify SDK for Python is the official library for creating Apify Actors. Actors are serverless cloud programs that can perform various web scraping and automation tasks. This SDK provides tools for Actor lifecycle management, local storage emulation, and event handling, allowing developers to build scalable data extraction solutions. It is actively maintained, with the current stable version being 3.3.2.
pip install apifyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a simple Apify Actor that fetches URLs from an input, scrapes their titles using HTTPX and BeautifulSoup, and pushes the extracted data to the default dataset. It utilizes the `async with Actor:` context manager for proper lifecycle management and `RequestQueue` for managing URLs.
Consult the official 'Upgrading to v3' documentation. Replace removed methods with their v3 counterparts (e.g., `open` for storages, `get_metadata` for info). Adapt to `crawlee` v1.0 storage API changes.
Refactor your Actor's main logic to use `async with Actor:`: `async def main(): async with Actor: # Your Actor logic here`. This ensures proper lifecycle management and resource handling.
Ensure your Python environment is running version 3.10 or newer. Upgrade Python if necessary.
If you need to preserve local storage between runs for testing or specific workflows, you can disable automatic purging by passing `purge=False` to the Actor initialization, e.g., `async with Actor(purge=False):`.
Avoid using mutable objects as default arguments. Instead, use `None` as the default and initialize the mutable object inside the function if `None` is detected: `def func(arg=None): arg = arg if arg is not None else []`.
Refactor your Actor's entry point to use the recommended asynchronous context manager: `async def main(): async with Actor: # your logic` and run with `asyncio.run(main())`.
Pass an `apify.Request` object to `add_request()`. For simple URLs, you can often pass a string, but for more complex requests, create a `Request` object: `from apify import Request; await request_queue.add_request(Request(url='http://example.com'))`.
Review your Actor's `INPUT_SCHEMA.json` and ensure that the input data strictly matches the defined schema, including data types, required fields, and patterns. Use the Apify Console's visual input schema editor or the `apify validate-schema` CLI command to check validity.