跳到正文
AI 工具3 credits

extract_structured

为提取器提供 JSON Schema 和自然语言提示词。LLM 会读取页面并返回符合你 schema 的数据。当未配置 LLM 提供方时,它会使用你的提示回退到 CSS 选择器提取。

使用场景

schema 优先的商品提取

只需定义一次你想要的字段;LLM 会将任意电商站点映射到你的 schema。

简历与文档解析

将候选人姓名、技能和工作经历直接提取到一个类型化对象中。

知识图谱填充

从文章中提取实体和关系,生成供图加载器使用的结构化 JSON。

Endpoint

POST/api/v1/tools/extract_structured
Auth Required
Free 计划 1 req/s
3 credits

Parameters

NameTypeRequiredDefaultDescription
url
stringRequired-
要从中提取数据的 URL
Example: https://example.com/product/123
schema
objectRequired-
描述要提取数据的 JSON Schema
Example: {"type":"object","properties":{"title":{"type":"string"},"price":{"type":"number"}},"required":["title"]}
prompt
stringOptional-
指导 LLM 提取的自然语言指令
Example: Extract the product name, current price, and whether it is in stock
llmConfig
objectOptional-
可选的 LLM 提供方配置(provider、apiKey)。省略则使用 CSS 选择器回退。
Example: {"provider": "openai", "apiKey": "sk-..."}
selectorHints
objectOptional-
用于指导提取的 CSS 选择器提示(也供选择器回退使用)
Example: {"title": "h1.product-title", "price": ".price"}
fallbackToSelectors
booleanOptionaltrue
当 LLM 不可用时回退到 CSS 选择器提取
Example: true
respect_robots
booleanOptionaltrue
遵守目标站点的 robots.txt。保持 `true` 时,robots.txt 对 `CrawlForge` 禁止的路径会在抓取之前以 403 拒绝,且不扣除 credits。仅在你与目标站点另有约定时才设为 `false`——此时响应会带上一条 `warnings`,并且该覆盖会记录到你的 API key 上。
Example: true

请求示例

cURL — LLM 提取

terminalBash
curl -X POST https://crawlforge.dev/api/v1/tools/extract_structured \
  -H "X-API-Key: cf_test_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com/product/123",
    "schema": {
      "type": "object",
      "properties": {
        "title": { "type": "string" },
        "price": { "type": "number" },
        "in_stock": { "type": "boolean" }
      },
      "required": ["title", "price"]
    },
    "prompt": "Extract the product name, price in USD, and availability",
    "llmConfig": { "provider": "openai", "apiKey": "sk-..." }
  }'

TypeScript — 选择器回退

extractStructured.tsTypescript
// npm install crawlforge-sdk
import { CrawlForge } from 'crawlforge-sdk';

const client = new CrawlForge({ apiKey: process.env.CRAWLFORGE_API_KEY });

const result = await client.extractStructured({
  url: 'https://example.com/product/123',
  schema: {
    type: 'object',
    properties: {
      title: { type: 'string' },
      price: { type: 'number' },
    },
    required: ['title'],
  },
  selectorHints: {
    title: 'h1.product-title',
    price: '.price-value',
  },
  fallbackToSelectors: true,
});

// result.data is untyped in crawlforge-sdk 0.1 — its shape is the Response Example below.
const { data: product } = result.data as { data: { title: string; price: number } };
console.log(product.title, product.price);

Python

extract_structured.pyPython
# pip install crawlforge
from crawlforge import CrawlForge

client = CrawlForge()  # reads CRAWLFORGE_API_KEY

result = client.extract_structured(
    url='https://example.com/article/42',
    schema={
        'type': 'object',
        'properties': {
            'headline': {'type': 'string'},
            'author': {'type': 'string'},
            'published_at': {'type': 'string'},
            'tags': {'type': 'array'},
        },
        'required': ['headline'],
    },
    prompt='Extract headline, author, publish date (ISO 8601), and tags',
)

# result.data is a plain dict — its shape is the Response Example below.
print(result.data['data'])

响应示例

200 OK1.2s
{
"success": true,
"data": {
"url": "https://example.com/product/123",
"data": {
"title": "Premium Wireless Headphones",
"price": 299.99,
"in_stock": true
},
"extraction": {
"method_by_field": {
"title": "selector",
"price": "json-ld",
"in_stock": "meta"
},
"llm_used": false,
"note": "Selector/structured-data extraction; LLM-guided extraction is available via the CrawlForge MCP server"
},
"extracted_at": "2026-08-26T14:30:00.000Z"
},
"credits_used": 3,
"credits_remaining": 997,
"processing_time": 1240
}
Field Descriptions
data.data您的 schema 中每个属性对应一个键。`number` 或 `integer` 属性取所找到文本中的第一个数字(`"4.5 out of 5"` 为 4.5,`"$1,299.00"` 为 1299;`integer` 仅在该数字为整数时转换);不含数字的文本按原样返回。未能找到的字段为 null。
data.extraction.method_by_field每个字段的取值来源:`selector` 来自 selectorHints 条目,`json-ld` 来自结构化数据,`meta` 来自 meta 标签,`none` 表示没有任何匹配。
data.extraction.llm_used在托管 REST API 上始终为 false——提取基于选择器与结构化数据,绝非生成式。
data.extraction.note重申可在何处使用由 LLM 引导的提取。
data.extracted_at本次提取的 ISO 8601 时间戳。

错误处理

被 robots.txt 拦截(403 Forbidden)

目标站点的 robots.txt 对 CrawlForge 禁止了该路径。若你与目标站点另有约定,可设置 respect_robots: false 予以覆盖——该覆盖会记录到你的 API key 上。该覆盖不适用于列入 CrawlForge 永久排除名单的主机——无论 respect_robots 取何值,这类主机一律被拒绝。

credit 费用

3 credits
每次请求 3 credits
无论本次调用使用 LLM 还是选择器回退,均固定 3 credits。

提示: 当你已有稳定的选择器、无需 LLM 灵活性时,可搭配 scrape_structured(2 credits,仅 CSS)使用。

相关工具