|
| 1 | +--- |
| 2 | +id: quick-start |
| 3 | +title: Quick start |
| 4 | +sidebar_label: Quick start |
| 5 | +description: 'Get started with the Apify SDK for Python by creating your first Actor and learning the basics.' |
| 6 | +--- |
| 7 | + |
| 8 | +Learn how to create and run Actors using the Apify SDK for Python. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +import Tabs from '@theme/Tabs'; |
| 13 | +import TabItem from '@theme/TabItem'; |
| 14 | +import CodeBlock from '@theme/CodeBlock'; |
| 15 | + |
| 16 | +## Step 1: Create Actors |
| 17 | + |
| 18 | +To create and run Actors through Apify Console, see the [Console documentation](https://docs.apify.com/academy/getting-started/creating-actors#choose-your-template). |
| 19 | + |
| 20 | +To create a new Apify Actor on your computer, you can use the [Apify CLI](https://docs.apify.com/cli), and select one of the [Python Actor templates](https://apify.com/templates?category=python). |
| 21 | + |
| 22 | +For example, to create an Actor from the "[beta] Python SDK" template, you can use the [`apify create` command](https://docs.apify.com/cli/docs/reference#apify-create-actorname). |
| 23 | + |
| 24 | +```bash |
| 25 | +apify create my-first-actor --template python-start |
| 26 | +``` |
| 27 | + |
| 28 | +This will create a new folder called `my-first-actor`, download and extract the "Getting started with Python" Actor template there, create a virtual environment in `my-first-actor/.venv`, and install the Actor dependencies in it. |
| 29 | + |
| 30 | +## Step 2: Run Actors |
| 31 | + |
| 32 | +To run the Actor, you can use the [`apify run` command](https://docs.apify.com/cli/docs/reference#apify-run): |
| 33 | + |
| 34 | +```bash |
| 35 | +cd my-first-actor |
| 36 | +apify run |
| 37 | +``` |
| 38 | + |
| 39 | +This command: |
| 40 | + |
| 41 | +- Activates the virtual environment in `.venv` (if no other virtual environment is activated yet) |
| 42 | +- Starts the Actor with the appropriate environment variables for local running |
| 43 | +- Configures it to use local storages from the `storage` folder |
| 44 | + |
| 45 | +The Actor input, for example, will be in `storage/key_value_stores/default/INPUT.json`. |
| 46 | + |
| 47 | +## Step 3: Understand Actor structure |
| 48 | + |
| 49 | +All Python Actor templates follow the same structure. |
| 50 | + |
| 51 | +The `.actor` directory contains the [Actor configuration](https://docs.apify.com/platform/actors/development/actor-config), such as the Actor's definition and input schema, and the Dockerfile necessary to run the Actor on the Apify platform. |
| 52 | + |
| 53 | +The Actor's runtime dependencies are specified in the `requirements.txt` file, which follows the [standard requirements file format](https://pip.pypa.io/en/stable/reference/requirements-file-format/). |
| 54 | + |
| 55 | +The Actor's source code is in the `src` folder. This folder contains two important files: |
| 56 | + |
| 57 | +- `main.py` - which contains the main function of the Actor |
| 58 | +- `__main__.py` - which is the entrypoint of the Actor package, setting up the Actor [logger](../concepts/logging) and executing the Actor's main function via [`asyncio.run()`](https://docs.python.org/3/library/asyncio-runner.html#asyncio.run). |
| 59 | + |
| 60 | +<Tabs> |
| 61 | + <TabItem value="main.py" label="main.py" default> |
| 62 | + <CodeBlock language="python">{ |
| 63 | +`from apify import Actor |
| 64 | +${''} |
| 65 | +async def main(): |
| 66 | + async with Actor: |
| 67 | + Actor.log.info('Actor input:', await Actor.get_input()) |
| 68 | + await Actor.set_value('OUTPUT', 'Hello, world!')` |
| 69 | + }</CodeBlock> |
| 70 | + </TabItem> |
| 71 | + <TabItem value="__main__.py" label="__main.py__"> |
| 72 | + <CodeBlock language="python">{ |
| 73 | +`import asyncio |
| 74 | +import logging |
| 75 | +${''} |
| 76 | +from apify.log import ActorLogFormatter |
| 77 | +${''} |
| 78 | +from .main import main |
| 79 | +${''} |
| 80 | +handler = logging.StreamHandler() |
| 81 | +handler.setFormatter(ActorLogFormatter()) |
| 82 | +${''} |
| 83 | +apify_logger = logging.getLogger('apify') |
| 84 | +apify_logger.setLevel(logging.DEBUG) |
| 85 | +apify_logger.addHandler(handler) |
| 86 | +${''} |
| 87 | +asyncio.run(main())` |
| 88 | + }</CodeBlock> |
| 89 | + </TabItem> |
| 90 | +</Tabs> |
| 91 | + |
| 92 | +If you want to modify the Actor structure, you need to make sure that your Actor is executable as a module, via `python -m src`, as that is the command started by `apify run` in the Apify CLI. We recommend keeping the entrypoint for the Actor in the `src/__main__.py` file. |
| 93 | + |
| 94 | +## Step 4: Add dependencies {#adding-dependencies} |
| 95 | + |
| 96 | +Adding dependencies into the Actor is simple. |
| 97 | + |
| 98 | +First, add them in the [`requirements.txt`](https://pip.pypa.io/en/stable/reference/requirements-file-format/) file in the Actor source folder. |
| 99 | + |
| 100 | +Then activate the virtual environment in `.venv`: |
| 101 | + |
| 102 | +<Tabs groupId="operating-systems"> |
| 103 | + <TabItem value="unix" label="Linux / macOS" default> |
| 104 | + <CodeBlock language="bash">{ |
| 105 | +`source .venv/bin/activate` |
| 106 | + }</CodeBlock> |
| 107 | + </TabItem> |
| 108 | + <TabItem value="win" label="Windows"> |
| 109 | + <CodeBlock language="powershell">{ |
| 110 | +`.venv\\Scripts\\activate` |
| 111 | + }</CodeBlock> |
| 112 | + </TabItem> |
| 113 | +</Tabs> |
| 114 | + |
| 115 | +Then install the dependencies: |
| 116 | + |
| 117 | +```bash |
| 118 | +python -m pip install -r requirements.txt |
| 119 | +``` |
| 120 | + |
| 121 | +## Next steps |
| 122 | + |
| 123 | +### Guides |
| 124 | + |
| 125 | +To see how you can integrate the Apify SDK with some of the most popular web scraping libraries, check out our guides for working with: |
| 126 | + |
| 127 | +- [Requests or HTTPX](../guides/requests-and-httpx) |
| 128 | +- [Beautiful Soup](../guides/beautiful-soup) |
| 129 | +- [Playwright](../guides/playwright) |
| 130 | +- [Selenium](../guides/selenium) |
| 131 | +- [Scrapy](../guides/scrapy) |
| 132 | + |
| 133 | +### Concepts |
| 134 | + |
| 135 | +To learn more about the features of the Apify SDK and how to use them, check out the Concepts section in the sidebar, especially the guides for: |
| 136 | + |
| 137 | +- [Actor lifecycle](../concepts/actor-lifecycle) |
| 138 | +- [Working with storages](../concepts/storages) |
| 139 | +- [Handling Actor events](../concepts/actor-events) |
| 140 | +- [How to use proxies](../concepts/proxy-management) |
0 commit comments