关于 web-fetch
web-fetch 是一个网页内容抓取技能,按 Markdown 接口、站点选择器和 Bun 脚本的顺序提取内容。适用于将网页转换为便于阅读的文本,并针对已知站点提供选择器配置。
主要功能
- 01优先抓取 Markdown 内容
- 02按 CSS 选择器提取 HTML
- 03使用 Bun 脚本作为备用方案
- 04支持包含和排除选择器
- 05提供故障排查指引
适用场景
- 01提取文档网站的正文
- 02从网页中筛选指定内容区域
- 03选择器提取效果不佳时运行备用解析
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ alleneubank/ claude-code --skill web-fetch安装后在 Claude Code 中输入/web-fetch确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
.gitignorefetch.tspackage.json详细介绍
网页内容抓取
按以下顺序抓取网页内容:
- 优先使用原生 Markdown 端点(
content-type: text/markdown) - 对已知网站使用基于选择器的 HTML 提取
- 选择器提取失败时,使用捆绑的 Bun 回退脚本
前置条件
提取前检查所需工具:
command -v curl >/dev/null || echo "curl is required"
command -v html2markdown >/dev/null || echo "html2markdown is required for HTML extraction"
command -v bun >/dev/null || echo "bun is required for fetch.ts fallback"
安装捆绑脚本所需的 Bun 依赖:
cd ~/.claude/skills/web-fetch && bun install
默认工作流
对任意 URL,默认使用以下流程:
URL="<url>"
CONTENT_TYPE="$(curl -sIL "$URL" | awk -F': ' 'tolower($1)=="content-type"{print tolower($2)}' | tr -d '\r' | tail -1)"
if echo "$CONTENT_TYPE" | grep -q "markdown"; then
curl -sL "$URL"
else
curl -sL "$URL" \
| html2markdown \
--include-selector "article,main,[role=main]" \
--exclude-selector "nav,header,footer,script,style"
fi
已知网站选择器
| 网站 | 包含选择器 | 排除选择器 |
|---|---|---|
| platform.claude.com | #content-container | - |
| docs.anthropic.com | #content-container | - |
| developer.mozilla.org | article | - |
| github.com(文档) | article | nav,.sidebar |
| 通用 | article,main,[role=main] | nav,header,footer,script,style |
示例:
curl -sL "<url>" \
| html2markdown \
--include-selector "#content-container" \
--exclude-selector "nav,header,footer"
查找合适的选择器
当网站不在模式列表中时:
# Check what content containers exist
curl -s "<url>" | grep -o '<article[^>]*>\|<main[^>]*>\|id="[^"]*content[^"]*"' | head -10
# Test a selector
curl -sL "<url>" | html2markdown --include-selector "<selector>" | head -30
# Check line count
curl -sL "<url>" | html2markdown --include-selector "<selector>" | wc -l
通用回退脚本
当选择器产生的结果不理想时,运行捆绑的解析器:
bun ~/.claude/skills/web-fetch/fetch.ts "<url>"
如果当前已在技能目录中:
bun fetch.ts "<url>"
选项参考
--include-selector "CSS" # Keep only matching elements
--exclude-selector "CSS" # Remove matching elements
--domain "https://..." # Convert relative links to absolute
故障排除
使用选择器时输出为空:页面可能是原生 Markdown。先检查响应头:
curl -sIL "<url>" | grep -i '^content-type:'
选取了错误的内容:网站可能有多个 article/main 区域:
curl -s "<url>" | grep -o '<article[^>]*>'
找不到 html2markdown:安装后重试基于选择器的提取。
缺少 bun 或脚本依赖:运行 cd ~/.claude/skills/web-fetch && bun install。
缺少代码块:检查网站是否使用非标准代码格式。
由客户端渲染的内容:如果 HTML 只有“Loading...”占位文本,则内容由 JS 渲染。curl 和 Bun 脚本都无法提取;请使用基于浏览器的工具。
评论
支持 Markdown