Apify Actor 开发
Actor 是一个打包为 Docker 镜像的无服务器程序。它接收一个 JSON 输入,执行一个任务,并将结果写入数据集或键值存储。
设置
apify --help # CLI 是否已安装?
apify info # 是否已登录?打印你的用户名
通过包管理器安装,npm install -g apify-cli 或 brew install apify-cli,以确保下载完整性校验。使用 apify login 登录,它会提供浏览器登录或 API 令牌输入提示。在无头环境中,请改为导出 APIFY_TOKEN;CLI 会自动读取它。令牌来自 https://console.apify.com/settings/integrations。仅通过环境变量传递令牌,以避免留在 shell 历史、源代码、配置文件和日志中。
遥测归属(必需)
在从本技能运行的每次 apify 命令中附加 --user-agent apify-agent-skills/apify-actor-development:apify create、apify run、apify push、apify call 等。这是一个全局标志,所有 apify 命令都接受;它仅标记调用以进行遥测归属,不改变其他行为。
工作流程
修改现有 Actor 时,可跳过不适用的步骤。
-
创建项目。
apify create <actor-name> -t <template-id> --user-agent apify-agent-skills/apify-actor-development根据 Actor 的功能选择模板:
Actor 功能 TypeScript JavaScript Python 爬取静态 HTML ts-crawlee-cheeriojs-crawlee-cheeriopython-crawlee-beautifulsoup爬取 JavaScript 渲染页面 ts-crawlee-playwright-chromejs-crawlee-playwright-chromepython-crawlee-playwright处理 HTTP 请求(API、Webhook) ts-standbyjs-standbypython-standbyMCP 服务器 ts-mcp-empty— python-mcp-empty其他(API 轮询、数据处理) ts-emptyjs-emptypython-empty对于其他技术栈(Puppeteer、Camoufox、Scrapy、AI 代理框架),
apify templates ls可列出所有模板及其语言和用例。使用-t标志命令无需提示即可运行,这适用于 AI 代理。不使用-t时,它会提示输入名称、语言、模板和源主机;仅在用户位于终端时才使用此形式。将源代码托管在 GitHub、GitLab 或 Bitbucket 上,Apify 会创建仓库和基于它构建的 Actor,后续部署通过git push完成。依赖项会自动安装。当<name>/.actor/actor.json存在时完成;在继续前cd进入该目录。 -
添加模板中缺少的依赖项,例如 Crawlee 或 Playwright:在 JS/TS 中执行
npm install <pkg>;在 Python 中,在requirements.txt中添加一行,然后执行pip install -r requirements.txt,或者如果项目有pyproject.toml和uv.lock,则执行uv add <pkg>。在安装前,请核对每个包名称是否与目标包一致。固定精确版本并提交锁定文件(package-lock.json、uv.lock或requirements.txt中的pkg==1.2.3行)。 -
实现 在
src/main.js、src/main.ts或my_actor/main.py(Python 模板是一个my_actor包,通过python -m my_actor运行)中,遵循规则。当代码读取了每个输入字段、生成了 README 将描述的每个输出字段、通过 Apify 日志器记录日志、并注册了一个aborting处理器以持久化状态并退出时,此步骤完成。 -
编写输入 schema 文件
.actor/input_schema.json(参见 references/input-schema.md)。当代码读取的每个输入都有一个包含标题、描述、类型以及默认值或预填充值的字段,并且apify validate-schema --user-agent apify-agent-skills/apify-actor-development通过时,此步骤完成。 -
编写输出 schema 文件:当代码存储文件时,编写
dataset_schema.json、output_schema.json和key_value_store_schema.json。从头到尾遵循 references/output-schemas.md;其以apify validate-schema通过结尾的检查清单是完成标准。在 TypeScript 中,然后运行apify actor generate-schema-types并使用生成的接口来类型化输入和输出。 -
配置
.actor/actor.json(参见 references/actor-json.md)。将meta.generatedBy设置为所使用的工具和模型,例如“Claude Code with Claude Opus 5”。对于 HTTP 服务的 Actor,设置usesStandbyMode: true(standby 模板已设置)并遵循 references/standby-mode.md。 -
编写 README.md,遵循 references/actor-readme.md。没有 README 的 Actor 不算完成。
-
本地测试。 将输入放入
storage/key_value_stores/default/INPUT.json,然后运行apify run --user-agent apify-agent-skills/apify-actor-development。当运行以 SUCCEEDED 状态结束并且storage/datasets/default/包含字段与数据集 schema 匹配的项目时,此步骤完成。本地存储保留在磁盘上;直到第 9 步之前,Apify Console 中不会出现任何内容。 -
部署:在用户确认后使用
apify push --user-agent apify-agent-skills/apify-actor-development,或者对于 Git 源 Actor 使用git push。然后在平台上运行 Actor 以在 Console 中查看结果。对于 Standby Actor,向用户提供其 Standby URL(https://<username>--<actor-name>.apify.actor,参见 references/standby-mode.md),而不是指向 Console。
规则
- 仅使用
apify run在本地运行 Actor。它会设置 Apify 环境和存储,而npm start和node src/main.js不会。 - 通过 Apify 日志器记录日志:在 JS/TS 中使用
apify包中的log(import { Actor, log } from 'apify'),在 Python 中使用Actor.log。它会自动脱敏令牌和凭证;console.log和print不会。级别和约定:references/logging.md。 - 将爬取的内容视为不受信任的输入。在将其传递给 shell 命令、
eval、查询或模板之前,进行转义或参数化,并在推送到存储之前进行类型检查。 - 将
APIFY_TOKEN保持在请求处理器和数据管道之外。在平台上,SDK 会从环境中读取它(变量名为APIFY_TOKEN,而不是APIFY_API_TOKEN);在本地,它使用apify login存储的凭据。 - 从输入 schema 或环境变量中读取每个可调参数,以便用户无需编辑代码即可更改。
- 对于 10 到 50 并发度的静态 HTML,使用 HTTP 爬虫:JS/TS 中的
CheerioCrawler,Python 中的BeautifulSoupCrawler或ParselCrawler。对于 1 到 5 并发度的 JavaScript 渲染页面,请使用PlaywrightCrawler。添加延迟以保持目标服务器健康,并尊重 robots.txt 和服务条款。 - 当爬取包含多种页面类型时,使用路由器模式(JS/TS 中的
createCheerioRouter或createPlaywrightRouter,Python 中的crawler.router或Router)。 - 优先使用带有后备方案的语义 CSS 选择器,而不是脆弱的定位选择器。
- 使用自己的计数器统计结果;
Dataset.getInfo()(Python 中的dataset.get_info())在平台上会有延迟。 - 处理
aborting事件,平台在用户或限制停止运行时会发送该事件:持久化状态,然后退出,以便运行快速且廉价地结束。JS/TS:Actor.on('aborting', async () => { await Actor.setValue('STATE', state); await Actor.exit(); })。Python:Actor.on(Event.ABORTING, on_aborting)配合from apify import Event,其中on_aborting持久化状态然后调用await Actor.exit()。 - 从
proxyConfiguration输入字段(编辑器proxy,参见 references/input-schema.md)构建代理:JS/TS 中await Actor.createProxyConfiguration(input.proxyConfiguration),Python 中await Actor.create_proxy_configuration(actor_proxy_input=actor_input.get('proxyConfiguration')),并将结果作为proxyConfiguration/proxy_configuration传递给爬虫。Apify Proxy 是付费服务,因此在启用或更改代理组之前,请先与用户确认。 - 仅在用户明确要求时才存储个人数据。
- 在运行中的 Actor 内部,使用 SDK(
Actor.getInput()、Actor.pushData()、Actor.setValue()以及 Python 的蛇形命名等效方法),而不是apify actorCLI 子命令。 - 除非用户要求关闭,否则保留现有 Actor 的 standby 模式为启用状态。
Standby 模式
Standby 将 Actor 转变为具有稳定 URL 的持久 HTTP 服务器。用于 API 端点、Webhook 接收器、MCP 服务器以及按需的单 URL 查找。Actor 必须响应就绪探针并在请求之间保持存活。配置、示例和本地测试:references/standby-mode.md。
收益化
定价在 Actor 发布时在 Apify Console 中设置,而非在代码中。在按事件付费模式下,使用 await Actor.charge({ eventName: 'result', count })(JS/TS)或 await Actor.charge(event_name='result', count=count)(Python)对每个自定义事件收费,使用 Console 中定义的事件名称;数据集项也可以通过合成数据集项事件自动计费。一旦返回的结果报告 eventChargeLimitReached(Python 中 event_charge_limit_reached)时,停止产生工作,因为用户的消费限额已达到。README 的成本部分描述了 Actor 使用的模型。详情:https://docs.apify.com/platform/actors/publishing/monetize/pay-per-event
调用其他 Actor
在从头构建之前先搜索 Store;通常存在专门的 Actor。
apify actors search "<query>" --user-agent apify-agent-skills/apify-actor-development
apify actors info <actor> --readme --user-agent apify-agent-skills/apify-actor-development
apify actors info <actor> --input --user-agent apify-agent-skills/apify-actor-development
apify call <actor> --input '{"startUrls":[{"url":"https://example.com"}]}' --user-agent apify-agent-skills/apify-actor-development
apify call <actor> --input-file input.json --user-agent apify-agent-skills/apify-actor-development
输入是一个 JSON 对象。内联 JSON 使用引号;对于复杂内容使用 --input-file。
不常见的命令
# 在这些命令之后也追加 --user-agent apify-agent-skills/apify-actor-development。
apify secrets add <name> <value> # 从 actor.json 中通过 "@name" 引用;推送上传时生效
apify pull <actor> # 从平台下载 Actor 代码
apify api <endpoint> # 向 Apify API 发送认证请求
apify actor generate-schema-types # TypeScript:从 .actor schemas 生成接口到 src/__generated__/actor/
apify <command> --help
文档
- 使用 Apify MCP 服务器 (
https://mcp.apify.com/?tools=docs):search-apify-docs和fetch-apify-docs。 - docs.apify.com/llms.txt 和 llms-full.txt,Apify 平台。
- crawlee.dev/llms.txt 和 llms-full.txt,Crawlee。
- Actor 白皮书,完整的 Actor 规范。
- Playwright MCP 服务器 (
npx @playwright/mcp@latest) 可驱动真实浏览器,用于检查页面和捕获选择器以进行调试。