>Apify Actors 是云端程序,专为各种网页抓取、爬虫和数据提取任务而设计。这些 Actor 促进自动化的网络数据收集,使用户能够高效地提取、处理和存储信息。Actor 可用于执行诸如抓取电商网站产品详情、监控价格变化或收集搜索引擎结果等任务。它们可与 Apify Datasets无缝集成,允许将 Actor 收集的结构化数据以 JSON、CSV 或 Excel 等格式存储、管理和导出,以供进一步分析或使用。
概述
本笔记本将引导您使用 Apify Actors 与 LangChain 结合来自动化网页抓取和数据提取。该 langchain-apify 包将 Apify 的云端工具与 LangChain 代理集成,实现高效的数据收集和处理,以支持 AI 应用。
集成详情
| 类 | 包 | 可序列化 | JS 支持 | 版本 |
|---|---|---|---|---|
ApifyActorsTool | langchain-apify | ✅ | ✅ | !PyPI - 版本 |
工具特性
| 返回产物 | 原生异步 | 返回数据 | 定价 |
|---|---|---|---|
| ❌ | ✅ | Actor 输出(因 Actor 而异) | 按量付费, 免费套餐可用 |
设置
此集成位于 langchain-apify 包中。该包可以使用 pip 安装。
pip install langchain-apify
前提条件
os.environ["APIFY_TOKEN"] = "your-apify-token"
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
定价
Apify 采用按量付费定价,提供免费套餐。 定价因 Actor 而异——有些 Actor 是免费的(您只需支付平台使用费),而有些则按结果或事件收费。
实例化
在这里我们实例化 ApifyActorsTool 以便能够调用 RAG Web Browser Apify Actor。此 Actor 为 AI 和 LLM 应用提供网页浏览功能,类似于 ChatGPT 中的网页浏览功能。任何来自 Apify Store 的 Actor 都可以以这种方式使用。
from langchain_apify import ApifyActorsTool
tool = ApifyActorsTool("apify/rag-web-browser")
调用
ApifyActorsTool 接受一个参数,即 run_input ——一个字典,作为运行输入传递给 Actor。运行输入 schema 文档可以在 Actor 详情页面的输入部分找到。参见 RAG Web Browser 输入 schema.
tool.invoke({"run_input": {"query": "what is apify?", "maxResults": 2}})
链式调用
我们可以将创建的工具提供给 代理当被要求搜索信息时,代理将调用 Apify Actor,它将搜索网络,然后返回搜索结果。
pip install langgraph langchain-openai
from langchain.messages import ToolMessage
from langchain_openai import ChatOpenAI
from langchain.agents import create_agent
model = ChatOpenAI(model="gpt-5-mini")
tools = [tool]
graph = create_agent(model, tools=tools)
inputs = {"messages": [("user", "search for what is Apify")]}
stream = graph.stream_events(inputs, version="v3")
for snapshot in stream.values:
message = snapshot["messages"][-1]
# skip tool messages
if isinstance(message, ToolMessage):
continue
message.pretty_print()
================================ Human Message =================================
search for what is Apify
================================== Ai Message ==================================
Tool Calls:
apify_actor_apify_rag-web-browser (call_27mjHLzDzwa5ZaHWCMH510lm)
Call ID: call_27mjHLzDzwa5ZaHWCMH510lm
Args:
run_input: {"run_input":{"query":"Apify","maxResults":3,"outputFormats":["markdown"]}}
================================== Ai Message ==================================
Apify is a comprehensive platform for web scraping, browser automation, and data extraction. It offers a wide array of tools and services that cater to developers and businesses looking to extract data from websites efficiently and effectively. Here's an overview of Apify:
1. **Ecosystem and Tools**:
- Apify provides an ecosystem where developers can build, deploy, and publish data extraction and web automation tools called Actors.
- The platform supports various use cases such as extracting data from social media platforms, conducting automated browser-based tasks, and more.
2. **Offerings**:
- Apify offers over 10,000 ready-made scraping tools and code templates.
- Users can also build custom solutions or hire Apify's professional services for more tailored data extraction needs.
3. **Technology and Integration**:
- The platform supports integration with popular tools and services like Zapier, GitHub, Google Sheets, Pinecone, and more.
- Apify supports open-source tools and technologies such as JavaScript, Python, Puppeteer, Playwright, Selenium, and its own Crawlee library for web crawling and browser automation.
4. **Community and Learning**:
- Apify hosts a community on Discord where developers can get help and share expertise.
- It offers educational resources through the Web Scraping Academy to help users become proficient in data scraping and automation.
5. **Enterprise Solutions**:
- Apify provides enterprise-grade web data extraction solutions with high reliability, 99.95% uptime, and compliance with SOC2, GDPR, and CCPA standards.
For more information, you can visit [Apify's official website](https://apify.com/) or their [GitHub page](https://github.com/apify) which contains their code repositories and further details about their projects.
更多 Actor 示例
Apify Store 包含数千个预构建的 Actor。以下是其他热门 Actor 的示例:
Instagram 抓取工具
from langchain_apify import ApifyActorsTool
instagram_tool = ApifyActorsTool("apify/instagram-scraper")
# Scrape Instagram posts
result = instagram_tool.invoke({
"run_input": {
"directUrls": ["https://www.instagram.com/humansofny/"],
"resultsLimit": 10
}
})
Google 搜索结果抓取工具
google_search_tool = ApifyActorsTool("apify/google-search-scraper")
# Scrape Google Search results
result = google_search_tool.invoke({
"run_input": {
"queries": "langchain python tutorial",
"maxPagesPerQuery": 1
}
})
浏览 Apify Store 以发现更多适合您使用场景的 Actor。
何时使用 Apify
当您需要以下功能时,Apify 是理想选择:
- 访问数千个预构建 Actor 涵盖多个平台(社交媒体、电子商务、搜索引擎等)
- 自定义网页抓取和自动化工作流 超越简单搜索
- 无需基础设施的抓取 (无服务器平台处理扩展和维护)
- 灵活的 Actor 生态系统 – 运行 Apify Store 中的任何 Actor
API 参考
有关此集成的详细使用说明,请参阅 Git 仓库 或 Apify 集成文档.
使用 Apify MCP Server
不确定使用哪个 Actor 或需要哪些参数? Apify MCP(模型上下文协议)服务器 可以帮助您发现可用的 Actor、探索其输入模式,并通过模型上下文协议了解参数要求。
要将 Apify MCP server 与 LangChain 结合使用:
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain.agents import create_agent
client = MultiServerMCPClient({
"apify": {
"transport": "http",
"url": "https://mcp.apify.com",
"headers": {
"Authorization": f"Bearer {os.environ['APIFY_TOKEN']}",
},
}
})
tools = await client.get_tools()
agent = create_agent("gpt-5-mini", tools)
更多信息,请参阅 LangChain MCP 文档 和 Apify MCP server.