使用场景
生成站点地图
通过提取所有内部链接构建全面的站点地图
为爬取发现链接
在网页爬取工作流中找出所有要访问的 URL
内部链接分析
分析内部链接结构以进行 SEO 优化
失效链接检测
查找并校验页面上的所有链接
Endpoint
POST
/api/v1/tools/extract_linksAuth Required
Free 计划 1 req/s
1 credit
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
url | string | Optional | - | 要抓取并从中提取链接的页面。`url` 与 `html` 二者必填其一。 Example: https://example.com |
html | string | Optional | - | 直接解析的原始 HTML,替代抓取。请配合 `base_url` 使用,以便相对 href 能够解析。 Example: <a href='/about'>About</a> |
base_url | string | Optional | - | 解析相对链接所用的基地址。抓取时默认为 `url`;传入 `html` 时请一并提供。 Example: https://example.com |
include_internal | boolean | Optional | true | 包含指向同一主机的链接。 Example: true |
include_external | boolean | Optional | true | 包含指向其他主机的链接。设为 false 可只保留内部链接。 Example: true |
filter_domains | array | Optional | - | 仅保留主机与这些域名之一匹配的链接。 Example: ["docs.example.com"] |
include_anchors | boolean | Optional | false | 包含形如 `#section` 的纯片段链接。 Example: false |
deduplicate | boolean | Optional | true | 将重复的 URL 合并为一条。 Example: true |
include_metadata | boolean | Optional | true | 在 URL 之外一并返回每个链接的锚文本与属性。 Example: true |
respect_robots | boolean | Optional | true | 遵守目标站点的 robots.txt。保持 `true` 时,robots.txt 对 `CrawlForge` 禁止的路径会在抓取之前以 403 拒绝,且不扣除 credits。仅在你与目标站点另有约定时才设为 `false`——此时响应会带上一条 `warnings`,并且该覆盖会记录到你的 API key 上。 Example: true |
请求示例
cURL - 提取所有链接
terminalBash
curl -X POST https://crawlforge.dev/api/v1/tools/extract_links \
-H "X-API-Key: cf_test_YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{
"url": "https://example.com",
"include_internal": true,
"include_external": false,
"deduplicate": true
}'TypeScript - 仅内部链接
extractLinks.tsTypescript
const response = await fetch('https://crawlforge.dev/api/v1/tools/extract_links', {
method: 'POST',
headers: {
'X-API-Key': process.env.CRAWLFORGE_API_KEY!,
'Content-Type': 'application/json',
},
body: JSON.stringify({
url: 'https://example.com',
// Internal-only: keep include_internal, switch include_external off.
include_internal: true,
include_external: false,
deduplicate: true,
}),
});
const payload = await response.json();
if (!response.ok) {
throw new Error(payload.error.code + ': ' + payload.error.message);
}
const result = payload.data;
console.log('Base URL:', result.base_url);
console.log('Extracted from:', result.extracted_from);
for (const link of result.links) console.log(link);
// Narrow to specific hosts instead of a blanket internal/external split.
const docsOnly = await fetch('https://crawlforge.dev/api/v1/tools/extract_links', {
method: 'POST',
headers: {
'X-API-Key': process.env.CRAWLFORGE_API_KEY!,
'Content-Type': 'application/json',
},
body: JSON.stringify({
url: 'https://example.com',
filter_domains: ['docs.example.com'],
}),
});响应示例
200 OK210ms
{ "success": true, "data": { "url": "https://example.com", "links": [ { "text": "Home", "url": "https://example.com/", "internal": true }, { "text": "About", "url": "https://example.com/about", "internal": true }, { "text": "Products", "url": "https://example.com/products", "internal": true }, { "text": "External Resource", "url": "https://other-site.com", "internal": false } ], "total_links": 4, "internal_links": 3, "external_links": 1 }, "credits_used": 1, "credits_remaining": 999, "processing_time": 210}Field Descriptions
data.links所有已发现链接的数组,包含文本和 URLdata.total_links找到的链接总数data.internal_links指向同域名的链接数量data.external_links指向外部域名的链接数量错误处理
被 robots.txt 拦截(403 Forbidden)
目标站点的 robots.txt 对 CrawlForge 禁止了该路径。若你与目标站点另有约定,可设置 respect_robots: false 予以覆盖——该覆盖会记录到你的 API key 上。该覆盖不适用于列入 CrawlForge 永久排除名单的主机——无论 respect_robots 取何值,这类主机一律被拒绝。
相关工具
准备好提取链接了吗?免费注册,获取 1,000 credits。