Overview
The Apify SDK for Python is the official library for creating Apify Actors in Python. It provides everything you need to build an Actor and run it both locally and on the Apify platform. With the SDK, you can:
- Manage the Actor lifecycle: initialization, graceful shutdown, status messages, rebooting, and metamorphing.
- Work with datasets, key-value stores, and request queues, with automatic local emulation when running outside the platform.
- Read the Actor input, including automatic decryption of secret fields.
- React to platform events (system info, migration, abort) and persist state across migrations and restarts.
- Manage proxies, both Apify Proxy and your own, with session and tiered-proxy support.
- Start, call, and abort Actors and tasks, create webhooks, and reach the full Apify API client.
- Charge users with the pay-per-event pricing model.
- Integrate with Crawlee and Scrapy, with guides for Playwright and others.
import asyncio
import httpx
from bs4 import BeautifulSoup
from apify import Actor
async def main() -> None:
async with Actor:
actor_input = await Actor.get_input() or {}
url = actor_input.get('url', 'https://apify.com')
async with httpx.AsyncClient() as client:
response = await client.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
data = {
'url': url,
'title': soup.title.string if soup.title else None,
}
await Actor.push_data(data)
if __name__ == '__main__':
asyncio.run(main())
What are Actors
Actors are serverless cloud programs capable of performing tasks in a web browser, similar to what a human can do. These tasks can range from simple operations, such as filling out forms or unsubscribing from services, to complex jobs like scraping and processing large numbers of web pages.
Actors can be executed locally or on the Apify platform. The Apify platform lets you run Actors at scale and provides features for monitoring, scheduling, publishing, and monetizing them.
Quick start
To create and run Actors using Apify Console, check out Apify Console documentation. For creating and running Python Actors locally, refer to the quick start guide.
Explore the Guides section in the sidebar for a deeper understanding of the SDK's features and best practices.
Installation
The Apify SDK for Python requires Python version 3.10 or above. It is typically installed when you create a new Actor project using the Apify CLI. To install it manually in an existing project, use:
pip install apify
If you need to interact with the Apify API programmatically without creating Actors, use the Apify API client for Python instead.