Apify Client
The Apify Client for Python allows users to interact with the Apify platform, managing actors, datasets, key-value stores, and more. It provides both synchronous and asynchronous interfaces for web scraping and automation tasks. The current stable version is 2.5.0, with regular updates.
Warnings
- breaking Breaking Change: V1 to V2 API Client Initialization. In `v2.x`, the `ApifyClient` constructor directly accepts `token='YOUR_TOKEN'`, unifying the previous `ApifyClientSync` and `ApifyClientAsync` classes from `v1.x`. Users migrating from `v1.x` must adapt to this single client and direct token parameter.
- breaking Breaking Change: V1 to V2 Resource Access Patterns. Accessing Apify resources (Actors, Datasets, Key-Value Stores, etc.) moved from methods like `client.actors().get_or_create(...)` in `v1.x` to a more direct chainable pattern like `client.actor(...).get_or_create()` in `v2.x`.
- gotcha API Token Requirement: Most Apify API operations require an API token for authentication. If the `APIFY_TOKEN` environment variable is not set or the `token` parameter is not explicitly passed to the `ApifyClient` constructor, operations will typically result in authentication errors (e.g., 401 Unauthorized).
- gotcha Pagination for Large Datasets: When retrieving a large number of items from datasets or key-value stores, fetching all items at once can lead to memory exhaustion. Always use iterative methods or pagination parameters.
Install
-
pip install apify-client
Imports
- ApifyClient
from apify_client import ApifyClient
Quickstart
import os
from apify_client import ApifyClient
# Initialize the ApifyClient with your Apify API token.
# It's recommended to store your token in an environment variable named APIFY_TOKEN.
# Alternatively, pass it directly: apify_client = ApifyClient(token='YOUR_APIFY_TOKEN')
apify_client = ApifyClient(token=os.environ.get('APIFY_TOKEN', ''))
# Ensure an API token is provided
if not apify_client.token:
print("Warning: APIFY_TOKEN not found. Most operations will fail without authentication.")
# In a real application, you might raise an error or exit here.
# For this quickstart, we'll continue but expect failures for authenticated calls.
# Run a specific Actor (replace 'apify/hello-world' with your Actor ID)
try:
actor_run = apify_client.actor('apify/hello-world').call()
# Fetch results from the Actor's default dataset
print(f'Actor run data from dataset: {apify_client.dataset(actor_run["defaultDatasetId"]).list_items().items}')
# Or to fetch items one by one:
# for item in apify_client.dataset(actor_run["defaultDatasetId"]).iterate_items():
# print(item)
except Exception as e:
print(f"An error occurred while running the Actor or fetching data: {e}")
print("Please ensure your APIFY_TOKEN is valid and the 'apify/hello-world' actor is accessible.")