Skip to main content

Apify SDK for Python

Project description

Apify SDK for Python

The Apify SDK for Python is the official library to create Apify Actors in Python. It provides useful features like Actor lifecycle management, local storage emulation, and Actor event handling.

If you just need to access the Apify API from your Python applications, check out the Apify Client for Python instead.

Installation

The Apify SDK for Python is available on PyPI as the apify package. For default installation, using Pip, run the following:

pip install apify

For users interested in integrating Apify with Scrapy, we provide a package extra called scrapy. To install Apify with the scrapy extra, use the following command:

pip install apify[scrapy]

Documentation

For usage instructions, check the documentation on Apify Docs.

Examples

Below are few examples demonstrating how to use the Apify SDK with some web scraping-related libraries.

Apify SDK with HTTPX and BeautifulSoup

This example illustrates how to integrate the Apify SDK with HTTPX and BeautifulSoup to scrape data from web pages.

from apify import Actor
from bs4 import BeautifulSoup
from httpx import AsyncClient


async def main() -> None:
    async with Actor:
        # Retrieve the Actor input, and use default values if not provided.
        actor_input = await Actor.get_input() or {}
        start_urls = actor_input.get('start_urls', [{'url': 'https://apify.com'}])

        # Open the default request queue for handling URLs to be processed.
        request_queue = await Actor.open_request_queue()

        # Enqueue the start URLs.
        for start_url in start_urls:
            url = start_url.get('url')
            await request_queue.add_request(url)

        # Process the URLs from the request queue.
        while request := await request_queue.fetch_next_request():
            Actor.log.info(f'Scraping {request.url} ...')

            # Fetch the HTTP response from the specified URL using HTTPX.
            async with AsyncClient() as client:
                response = await client.get(request.url)

            # Parse the HTML content using Beautiful Soup.
            soup = BeautifulSoup(response.content, 'html.parser')

            # Extract the desired data.
            data = {
                'url': actor_input['url'],
                'title': soup.title.string,
                'h1s': [h1.text for h1 in soup.find_all('h1')],
                'h2s': [h2.text for h2 in soup.find_all('h2')],
                'h3s': [h3.text for h3 in soup.find_all('h3')],
            }

            # Store the extracted data to the default dataset.
            await Actor.push_data(data)

Apify SDK with PlaywrightCrawler from Crawlee

This example demonstrates how to use the Apify SDK alongside PlaywrightCrawler from Crawlee to perform web scraping.

from apify import Actor, Request
from crawlee.playwright_crawler import PlaywrightCrawler, PlaywrightCrawlingContext


async def main() -> None:
    async with Actor:
        # Retrieve the Actor input, and use default values if not provided.
        actor_input = await Actor.get_input() or {}
        start_urls = [url.get('url') for url in actor_input.get('start_urls', [{'url': 'https://apify.com'}])]

        # Exit if no start URLs are provided.
        if not start_urls:
            Actor.log.info('No start URLs specified in Actor input, exiting...')
            await Actor.exit()

        # Create a crawler.
        crawler = PlaywrightCrawler(
            # Limit the crawl to max requests. Remove or increase it for crawling all links.
            max_requests_per_crawl=50,
            headless=True,
        )

        # Define a request handler, which will be called for every request.
        @crawler.router.default_handler
        async def request_handler(context: PlaywrightCrawlingContext) -> None:
            url = context.request.url
            Actor.log.info(f'Scraping {url}...')

            # Extract the desired data.
            data = {
                'url': context.request.url,
                'title': await context.page.title(),
                'h1s': [await h1.text_content() for h1 in await context.page.locator('h1').all()],
                'h2s': [await h2.text_content() for h2 in await context.page.locator('h2').all()],
                'h3s': [await h3.text_content() for h3 in await context.page.locator('h3').all()],
            }

            # Store the extracted data to the default dataset.
            await context.push_data(data)

            # Enqueue additional links found on the current page.
            await context.enqueue_links()

        # Run the crawler with the starting URLs.
        await crawler.run(start_urls)

What are Actors?

Actors are serverless cloud programs that can do almost anything a human can do in a web browser. They can do anything from small tasks such as filling in forms or unsubscribing from online services, all the way up to scraping and processing vast numbers of web pages.

They can be run either locally, or on the Apify platform, where you can run them at scale, monitor them, schedule them, or publish and monetize them.

If you're new to Apify, learn what is Apify in the Apify platform documentation.

Creating Actors

To create and run Actors through Apify Console, see the Console documentation.

To create and run Python Actors locally, check the documentation for how to create and run Python Actors locally.

Guides

To see how you can use the Apify SDK with other popular libraries used for web scraping, check out our guides for using Requests and HTTPX, Beautiful Soup, Playwright, Selenium, or Scrapy.

Usage concepts

To learn more about the features of the Apify SDK and how to use them, check out the Usage Concepts section in the sidebar, particularly the guides for the Actor lifecycle, working with storages, handling Actor events or how to use proxies.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

apify-2.0.2.tar.gz (40.3 kB view details)

Uploaded Source

Built Distribution

apify-2.0.2-py3-none-any.whl (47.6 kB view details)

Uploaded Python 3

File details

Details for the file apify-2.0.2.tar.gz.

File metadata

  • Download URL: apify-2.0.2.tar.gz
  • Upload date:
  • Size: 40.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for apify-2.0.2.tar.gz
Algorithm Hash digest
SHA256 396924d73de0d898401ef73f6b33bf3ce08bedd3a0866cb1f14671efc606878c
MD5 fc7b5a19b27a38caabf6a9e6750af537
BLAKE2b-256 c011e6b8c21ac5928473f2ce49eae70879c451912ed6a8df136d37aad7b6dd42

See more details on using hashes here.

Provenance

The following attestation bundles were made for apify-2.0.2.tar.gz:

Publisher: release.yaml on apify/apify-sdk-python

Attestations:

File details

Details for the file apify-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: apify-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 47.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for apify-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 65bca55aec6df524fa9b9297c4123a263e0338007ce02dd11e650de39d75f3f2
MD5 cdff26a6fe336de93c1b26d99b35b45b
BLAKE2b-256 d8edd6cc44fda8076654e9b6b6411f66f5e8a7b994f70e88732abef98368370d

See more details on using hashes here.

Provenance

The following attestation bundles were made for apify-2.0.2-py3-none-any.whl:

Publisher: release.yaml on apify/apify-sdk-python

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page