Apify Client

2.5.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Apify client, run a 'Hello World' actor, and retrieve its results from the default dataset. It emphasizes secure API token handling via environment variables.

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.")

view raw JSON →