/markdown 端点获取网页内容并将其转换为 Markdown 格式。你可以指定 URL 和可选参数以优化提取过程。
可通过两种方式使用此端点:
- REST API:创建自定义 API Token,并授予
Browser Rendering - Edit权限。 - Workers 绑定:通过 Workers 绑定 从 Cloudflare Worker 直接调用端点。无需 API token。
更多信息请参阅 Quick Actions:开始之前。
https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/markdown必须提供 url 或 html 之一:
url(字符串)html(字符串)
- 规范化内容以供下游处理(摘要、差异、嵌入)
- 保存文章或文档以供编辑或存储
- 去除样式/脚本并保留可读内容和链接
此示例获取网页的 Markdown 表示。
curl -X 'POST' 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/markdown' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <apiToken>' \
-d '{
"url": "https://example.com"
}'{
"success": true,
"result": "# Example Domain\n\nThis domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.\n\n[More information...](https://www.iana.org/domains/example)"
}import Cloudflare from "cloudflare";
const client = new Cloudflare({
apiToken: process.env["CLOUDFLARE_API_TOKEN"],
});
const markdown = await client.browserRendering.markdown.create({
account_id: process.env["CLOUDFLARE_ACCOUNT_ID"],
url: "https://developers.cloudflare.com/",
});
console.log(markdown);interface Env {
BROWSER: BrowserRun;
}
export default {
async fetch(request, env): Promise<Response> {
return await env.BROWSER.quickAction("markdown", {
url: "https://example.com",
});
},
} satisfies ExportedHandler<Env>;你可以直接提供原始 HTML 内容,而非通过指定 URL 获取内容。
curl -X 'POST' 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/markdown' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <apiToken>' \
-d '{
"html": "<div>Hello World</div>"
}'{
"success": true,
"result": "Hello World"
}你可以使用 rejectRequestPattern 参数优化 Markdown 提取。在此示例中,匹配给定正则表达式模式(如 CSS 文件)的请求将被排除。
curl -X 'POST' 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/markdown' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <apiToken>' \
-d '{
"url": "https://example.com",
"rejectRequestPattern": ["/^.*\\.(css)/"]
}'{
"success": true,
"result": "# Example Domain\n\nThis domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.\n\n[More information...](https://www.iana.org/domains/example)"
}对于 JavaScript 密集型页面或单页应用(SPA),默认的页面加载行为可能返回空或不完整的结果。这是因为浏览器在 JavaScript 完成渲染内容之前就认为页面已加载完毕。
最简单的解决方案是将 gotoOptions.waitUntil 参数设置为 networkidle0 或 networkidle2:
{
"url": "https://example.com",
"gotoOptions": {
"waitUntil": "networkidle0"
}
}如需更快响应,高级用户可使用 waitForSelector 等待特定元素,而非等待所有网络活动停止。这需要了解哪个 CSS 选择器表示所需内容已加载。更多详情,请参阅 Quick Actions 超时。
可在 JSON 请求体的顶层传入 userAgent 参数,在页面级别更改 user agent。当目标网站根据 user agent 返回不同内容时很有用。
如有疑问或遇到错误,请参阅 Browser Run 常见问题与故障排除指南。
- Workers AI AI.toMarkdown() 支持多种文档类型和摘要。
- Markdown for Agents 允许使用内容协商请求头为 Cloudflare zone 进行实时文档转换。