AgentQL 加载器
AgentQL's document loader 提供了使用 AgentQL 查询 从任意网页中提取结构化数据的功能。AgentQL 可以跨多个语言和网页使用,且不会因为时间推移或内容变更而失效。
概览
AgentQLLoader 需要以下两个参数:
url: 您想要提取数据的网页的URL。query: 要执行的 AgentQL 查询。了解如何在文档中编写AgentQL 查询,或在AgentQL Playground中尝试一个查询。
设置以下参数是可选的:<br>
api_key: 您的 AgentQL API密钥,来自dev.agentql.com。Optional。timeout: 等待请求的秒数,直到超时。默认值为900。is_stealth_mode_enabled: 是否启用实验性的反爬策略。此功能可能并非所有网站在任何时候都有效。启用此模式后,数据提取可能会花费更长的时间。默认值为False。wait_for: 等待页面加载以提取数据的秒数。默认值为0。is_scroll_to_bottom_enabled: 是否在提取数据前滚动到页面底部。默认值为False。mode:"standard"使用深度数据分析,而"fast"为了速度牺牲了一些分析的深度,并且对于大多数用例来说是足够的。点击此处了解关于模式的更多信息。 默认为"fast"。is_screenshot_enabled: 在提取数据之前是否需要截屏。结果将作为Base64字符串存储在'metadata'中。默认值为False。
AgentQLLoader 是通过 AgentQL 的 REST API 实现的
集成细节
| Class | 包 | 本地 | 序列化 | JS支持 |
|---|---|---|---|---|
| AgentQLLoader | langchain-agentql | ✅ | ❌ | ❌ |
加载器功能
| 来源 | 文档延迟加载 | 原生异步支持 |
|---|---|---|
| AgentQLLoader | ✅ | ❌ |
设置
要使用 AgentQL 文档加载器,您需要配置 AGENTQL_API_KEY 环境变量,或者使用 api_key 参数。您可以在我们的 开发门户 获取 API 密钥。
安装
安装 langchain-agentql。
%pip install -qU langchain_agentql
设置凭证
import os
os.environ["AGENTQL_API_KEY"] = "YOUR_AGENTQL_API_KEY"
初始化
接下来,实例化您的模型对象:
from langchain_agentql.document_loaders import AgentQLLoader
loader = AgentQLLoader(
url="https://www.agentql.com/blog",
query="""
{
posts[] {
title
url
date
author
}
}
""",
is_scroll_to_bottom_enabled=True,
)
加载
docs = loader.load()
docs[0]
Document(metadata={'request_id': 'bdb9dbe7-8a7f-427f-bc16-839ccc02cae6', 'generated_query': None, 'screenshot': None}, page_content="{'posts': [{'title': 'Launch Week Recap—make the web AI-ready', 'url': 'https://www.agentql.com/blog/2024-launch-week-recap', 'date': 'Nov 18, 2024', 'author': 'Rachel-Lee Nabors'}, {'title': 'Accurate data extraction from PDFs and images with AgentQL', 'url': 'https://www.agentql.com/blog/accurate-data-extraction-pdfs-images', 'date': 'Feb 1, 2025', 'author': 'Rachel-Lee Nabors'}, {'title': 'Introducing Scheduled Scraping Workflows', 'url': 'https://www.agentql.com/blog/scheduling', 'date': 'Dec 2, 2024', 'author': 'Rachel-Lee Nabors'}, {'title': 'Updates to Our Pricing Model', 'url': 'https://www.agentql.com/blog/2024-pricing-update', 'date': 'Nov 19, 2024', 'author': 'Rachel-Lee Nabors'}, {'title': 'Get data from any page: AgentQL’s REST API Endpoint—Launch week day 5', 'url': 'https://www.agentql.com/blog/data-rest-api', 'date': 'Nov 15, 2024', 'author': 'Rachel-Lee Nabors'}]}")
print(docs[0].metadata)
{'request_id': 'bdb9dbe7-8a7f-427f-bc16-839ccc02cae6', 'generated_query': None, 'screenshot': None}
懒加载
AgentQLLoader 目前一次只能加载一个 Document。因此,load() 和 lazy_load() 的行为相同:
pages = [doc for doc in loader.lazy_load()]
pages
[Document(metadata={'request_id': '06273abd-b2ef-4e15-b0ec-901cba7b4825', 'generated_query': None, 'screenshot': None}, page_content="{'posts': [{'title': 'Launch Week Recap—make the web AI-ready', 'url': 'https://www.agentql.com/blog/2024-launch-week-recap', 'date': 'Nov 18, 2024', 'author': 'Rachel-Lee Nabors'}, {'title': 'Accurate data extraction from PDFs and images with AgentQL', 'url': 'https://www.agentql.com/blog/accurate-data-extraction-pdfs-images', 'date': 'Feb 1, 2025', 'author': 'Rachel-Lee Nabors'}, {'title': 'Introducing Scheduled Scraping Workflows', 'url': 'https://www.agentql.com/blog/scheduling', 'date': 'Dec 2, 2024', 'author': 'Rachel-Lee Nabors'}, {'title': 'Updates to Our Pricing Model', 'url': 'https://www.agentql.com/blog/2024-pricing-update', 'date': 'Nov 19, 2024', 'author': 'Rachel-Lee Nabors'}, {'title': 'Get data from any page: AgentQL’s REST API Endpoint—Launch week day 5', 'url': 'https://www.agentql.com/blog/data-rest-api', 'date': 'Nov 15, 2024', 'author': 'Rachel-Lee Nabors'}]}")]
API 参考
对于如何使用此集成的更多信息,请参阅git 仓库或langchain 集成文档