网淘吧来吧,欢迎您!

Markdown Fetch技能使用说明

2026-03-31 新闻来源:网淘吧 围观:3
电脑广告
手机广告

Markdown Fetch - 网页抓取优化

背景

Cloudflare 推出Markdown for Agents功能:

  • AI 请求时返回 Markdown 格式
  • Token 消耗比 HTML 减少约 80%

使用方法

在需要网页抓取时,使用优化后的 fetch 函数:

Markdown Fetch

const { optimizedFetch } = require('./markdown-fetch');

const result = await optimizedFetch('https://example.com');
// result.markdown - Markdown 内容(如果有)
// result.html - HTML 内容(备用)
// result.tokensSaved - 节省的 tokens(如果有)

核心逻辑

async function optimizedFetch(url, options = {}) {
  const headers = {
    'Accept': 'text/markdown, text/html',
    ...options.headers
  };

  const response = await fetch(url, { ...options, headers });
  
  const contentType = response.headers.get('content-type');
  const xMarkdownTokens = response.headers.get('x-markdown-tokens');
  
  let result = {
    url,
    contentType,
    tokensSaved: xMarkdownTokens ? parseInt(xMarkdownTokens) : null
  };
  
  if (contentType.includes('text/markdown')) {
    result.markdown = await response.text();
    result.format = 'markdown';
  } else {
    result.html = await response.text();
    result.format = 'html';
  }
  
  return result;
}

响应处理

Content-Type处理方式
text/markdown直接使用,跳过 HTML 解析
text/html走原有解析逻辑

可选:x-markdown-tokens 日志

如果响应中有x-markdown-tokensheader,记录到日志:

if (result.tokensSaved) {
  console.log(`[Markdown Fetch] Token 节省: ${result.tokensSaved}`);
}

改动范围

  1. 找到所有 HTTP 请求(fetch/axios/request)
  2. 统一添加 header
  3. 响应处理加判断

测试验证

Find a Cloudflare-hosted website for testing:

curl -H "Accept: text/markdown, text/html" https://cloudflare-example.com

Acknowledged.content-type: text/markdownResponse.

免责申明
部分文章来自各大搜索引擎,如有侵权,请与我联系删除。
打赏

文章底部电脑广告
手机广告位-内容正文底部

相关文章

您是本站第393676名访客 今日有1篇新文章/评论