Defuddle 实战手册从 CLI 到 Node.js API 的 8 组对比测试全部测试在 macOS Node.js 22 环境下实际运行输出为真实终端记录。环境准备# 方式一全局安装npminstall-gdefuddle# 方式二npx 免安装运行本文使用此方式npx defuddle0.19.3--version# 输出: 0.19.3# Node.js API 使用需额外安装 DOM 解析器cd/tmpnpminstalllinkedom defuddle当前版本0.19.3MIT 协议TypeScript 编写。测试 1基础提取——博客文章转 Markdown目标从 Steph Ango 的博客提取一篇短文。npx defuddle0.19.3 parsehttps://stephango.com/saw--markdown实际输出When I learned to use a table saw, my teacher impressed upon me that the machine wants to cut fingers. Fear the saw! Powerful tools can do powerful things. If you want to make handmade wooden furniture you must cut wood. Your desire to have limbs and your desire to have furniture are not at odds if you learn to use tools safely. There *are* table saws that will stop at the touch of a finger, but I dont know of any chainsaw that wont cut an arm. There is a limit to how safe a tool can be before its function is crippled. We should not stop making powerful tools because they are dangerous. Rather, we should empower people to use powerful tools safely. I use the saw, I fear the saw. I have handmade furniture, and I have all my limbs.结果143 词102ms。导航栏、侧边栏、页脚全部干净移除正文完整。测试 2JSON 输出——提取元数据npx defuddle0.19.3 parsehttps://stephango.com/saw--json实际输出字段一览字段值titleUse the saw, fear the sawauthorSteph AngodescriptionIt’s hard to make a saw that cuts trees but not arms.domainstephango.comsiteSteph AngolanguageenwordCount143parseTime98mspublished2025-10-20T00:00:0000:00imagehttps://stephango.com/assets/card.pngfaviconhttps://stephango.com/favicon.icometaTags16 个 meta 标签含 og: 和 twitter:schemaOrgData空数组该页无结构化数据content804 字符的 HTML 正文--json适合脚本化处理一次拿到内容和全部元数据。测试 3Frontmatter 输出——知识库入库格式npx defuddle0.19.3 parsehttps://github.com/kepano/defuddle--markdown--frontmatter实际输出头部---title:kepano/defuddle: Get the main content of any page as Markdown.author:kepanosite:GitHubsource:https://github.com/kepano/defuddledomain:github.comlanguage:endescription:Get the main content of any page as Markdown...word_count:1627---正文紧随其后378 行 Markdown。这是 Obsidian Web Clipper 内部使用的格式——带 YAML 前言的 Markdown直接可以存入 Obsidian 笔记库。测试 4Debug 模式——追踪提取决策链npx defuddle0.19.3 parsehttps://stephango.com/saw--debug实际输出Defuddle: Content candidates: [ { element: ARTICLE, selector: html body main article, score: 478 }, { element: BODY, selector: html body, score: 459.8 }, { element: MAIN, selector: html body main, score: 422.5 } ] Defuddle: Removed hidden elements: 1 Defuddle: Removed clutter elements: { exactSelectors: 35, partialSelectors: 2, total: 37 } Defuddle: Removed non-content blocks: { count: 0 }关键信息3 个候选区域参与评分article以 478 分胜出37 个干扰元素通过选择器精确移除35 个精确匹配 2 个模糊匹配1 个隐藏元素被移除display:none的内容总耗时 102msdebug 模式是排查「为什么某段内容被误删」的第一手段。测试 5属性提取——只取一个字段# 只提取标题npx defuddle0.19.3 parsehttps://stephango.com/saw--propertytitle# 输出: Use the saw, fear the saw# 只提取作者npx defuddle0.19.3 parsehttps://stephango.com/saw--propertyauthor# 输出: Steph Ango# 只提取字数npx defuddle0.19.3 parsehttps://stephango.com/saw--propertywordCount# 输出: 143适合在脚本中只取元数据、不需要正文的场景。测试 6管道模式——curl 管道提取curl-sLhttps://stephango.com/saw|npx defuddle0.19.3 parse--markdown输出与测试 1 完全一致。管道模式的优势可以先用 curl 加自定义 header如 cookie、User-Agent抓取需要登录或反爬的页面再交给 Defuddle 提取。测试 7Node.js API——编程调用import{parseHTML}fromlinkedom;import{Defuddle}fromdefuddle/node;consthtml!DOCTYPE htmlhtmlheadtitleTest Article/title/headbody nava href/Home/a | a href/aboutAbout/a/nav article h1Hello World/h1 pThis is a test article with some content./p pAnother paragraph here with more text./p /article asideSubscribe to our newsletter!/aside footerCopyright 2026/footer /body/html;const{document}parseHTML(html);constresultawaitDefuddle(document,https://example.com/test,{markdown:true});console.log(result.title);// Test Articleconsole.log(result.wordCount);// 33console.log(result.content);实际输出{title:Test Article,wordCount:33,parseTime:46,content:## Hello World\n\nThis is a test article with some content. It has enough text to be recognized as the main content area.\n\nAnother paragraph here with more text to increase the content score.}导航栏nav、侧边栏aside、页脚footer全部自动移除只保留article内的正文。46ms 完成。手动指定 contentSelector当自动检测不准时可以用contentSelector精确指定正文区域constmanualawaitDefuddle(doc2,https://example.com/test,{markdown:true,contentSelector:.main-content// 跳过自动评分直接用 CSS 选择器});实测对比模式提取字数内容自动检测36 词正确提取.main-content区域手动选择器30 词同样提取.main-content但排除了更多噪音两种方式结果接近。手动选择器适合对特定网站结构已知、需要精确控制的场景。测试 8压缩率对比——原始 HTML vs Defuddle 输出# 原始 HTML 大小curl-sLhttps://stephango.com/saw-o/tmp/raw.htmlwc-c/tmp/raw.html# 输出: 11459 bytes# Defuddle Markdown 输出大小npx defuddle0.19.3 parsehttps://stephango.com/saw--markdown|wc-c# 输出: 750 bytes指标数值原始 HTML11,459 bytesDefuddle Markdown750 bytes压缩比15.3x这意味着喂给 LLM 时token 消耗只有原来的 1/15。对于 AI 上下文准备场景这直接影响成本。CLI 选项速查表选项简写用途--markdown-m输出 Markdown 格式--json-j输出 JSON含元数据正文--frontmatter-f输出带 YAML 前言的 Markdown--property name-p只提取指定字段--output file-o输出到文件--debug调试模式输出决策过程--lang code-l指定语言偏好--user-agent string-u自定义 User-Agent总结测试场景推荐用法关键指标快速提取正文npx defuddle parse url -m102ms / 143词批量脚本处理--jsonjq一次拿全元数据知识库入库--markdown --frontmatterYAML Markdown排查误删内容--debug评分移除明细只取元数据--property title单字段输出反爬/登录页curl管道自定义 header编程集成Node.js API linkedom46ms / 33词LLM 上下文准备--markdown15.3x 压缩Defuddle 的 CLI 覆盖了从快速提取到脚本化批处理的完整场景链。配合 Node.js API可以在任何需要网页内容提取的环节直接嵌入。项目地址https://github.com/kepano/defuddle安装npm install -g defuddle或npx defuddle parse url本文所有输出均为实际终端运行结果。