关于 content-harvest
content-harvest 是用于提取网页文章和博客正文的技能,适合需要保存干净文本的用户。它优先使用 reader 或 trafilatura,并提供基础解析作为后备方案。
主要功能
- 01提取文章正文并去除页面杂项
- 02优先使用 reader 或 trafilatura
- 03工具不可用时使用基础解析
- 04根据标题生成文件名
- 05保存文本并展示预览
适用场景
- 01将网页文章保存为文本文件
- 02清理新闻或博客页面内容
- 03提取学术文章正文
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ qodex-ai/ ai-agent-skills --skill content-harvest安装后在 Claude Code 中输入/content-harvest确认已加载。
详细介绍
文章提取器
此技能可从网页文章和博客文章中提取主要内容,去除导航、广告、订阅新闻简报表单及其他杂项,保存清晰易读的文本。
何时使用此技能
在用户出现以下情况时启用:
- 提供文章或博客 URL,并希望获取文本内容
- 要求“下载这篇文章”
- 希望“从 [URL] 提取内容”
- 要求“将这篇博客文章保存为文本”
- 需要不受干扰的纯净文章文本
工作原理
优先顺序:
- 检查工具是否已安装(reader 或 trafilatura)
- 使用最佳可用工具下载并提取文章
- 清理内容(删除多余空白,正确排版)
- 保存到文件,以文章标题作为文件名
- 确认保存位置并展示预览
安装检查
按以下顺序检查文章提取工具:
选项 1:reader(推荐,Mozilla 的 Readability)
command -v reader
如果未安装:
npm install -g @mozilla/readability-cli
# or
npm install -g reader-cli
选项 2:trafilatura(基于 Python,效果很好)
command -v trafilatura
如果未安装:
pip3 install trafilatura
选项 3:备用方案(curl + 简单解析)
如果没有可用工具,请使用基础的 curl + 文本提取(可靠性较低,但可用)
提取方法
方法 1:使用 reader(适用于大多数文章)
# Extract article
reader "URL" > article.txt
优点:
- 基于 Mozilla 的 Readability 算法
- 擅长去除杂项
- 保留文章结构
方法 2:使用 trafilatura(适用于博客和新闻)
# Extract article
trafilatura --URL "URL" --output-format txt > article.txt
# Or with more options
trafilatura --URL "URL" --output-format txt --no-comments --no-tables > article.txt
优点:
- 提取准确度很高
- 适用于各种网站结构
- 支持多种语言
选项:
--no-comments:跳过评论部分--no-tables:跳过数据表格--precision:优先保证准确率,而非召回率--recall:提取更多内容(可能会包含一些噪声)
方法 3:备用方案(curl + 基础解析)
# Download and extract basic content
curl -s "URL" | python3 -c "
from html.parser import HTMLParser
import sys
class ArticleExtractor(HTMLParser):
def __init__(self):
super().__init__()
self.in_content = False
self.content = []
self.skip_tags = {'script', 'style', 'nav', 'header', 'footer', 'aside'}
self.current_tag = None
def handle_starttag(self, tag, attrs):
if tag not in self.skip_tags:
if tag in {'p', 'article', 'main', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'}:
self.in_content = True
self.current_tag = tag
def handle_data(self, data):
if self.in_content and data.strip():
self.content.append(data.strip())
def get_content(self):
return '\n\n'.join(self.content)
parser = ArticleExtractor()
parser.feed(sys.stdin.read())
print(parser.get_content())
" > article.txt
**注意:**这种方法可靠性较低,但无需安装依赖即可使用。
获取文章标题
提取标题作为文件名:
使用 reader:
# reader outputs markdown with title at top
TITLE=$(reader "URL" | head -n 1 | sed 's/^# //')
使用 trafilatura:
# Get metadata including title
TITLE=$(trafilatura --URL "URL" --json | python3 -c "import json, sys; print(json.load(sys.stdin)['title'])")
使用 curl(备用方案):
TITLE=$(curl -s "URL" | grep -oP '<title>\K[^<]+' | sed 's/ - .*//' | sed 's/ | .*//')
创建文件名
清理标题以便用于文件系统:
# Get title
TITLE="Article Title from Website"
# Clean for filesystem (remove special chars, limit length)
FILENAME=$(echo "$TITLE" | tr '/' '-' | tr ':' '-' | tr '?' '' | tr '"' '' | tr '<' '' | tr '>' '' | tr '|' '-' | cut -c 1-100 | sed 's/ *$//')
# Add extension
FILENAME="${FILENAME}.txt"
完整工作流
ARTICLE_URL="https://example.com/article"
# Check for tools
if command -v reader &> /dev/null; then
TOOL="reader"
echo "Using reader (Mozilla Readability)"
elif command -v trafilatura &> /dev/null; then
TOOL="trafilatura"
echo "Using trafilatura"
else
TOOL="fallback"
echo "Using fallback method (may be less accurate)"
fi
# Extract article
case $TOOL in
reader)
# Get content
reader "$ARTICLE_URL" > temp_article.txt
# Get title (first line after # in markdown)
TITLE=$(head -n 1 temp_article.txt | sed 's/^# //')
;;
trafilatura)
# Get title from metadata
METADATA=$(trafilatura --URL "$ARTICLE_URL" --json)
TITLE=$(echo "$METADATA" | python3 -c "import json, sys; print(json.load(sys.stdin).get('title', 'Article'))")
# Get clean content
trafilatura --URL "$ARTICLE_URL" --output-format txt --no-comments > temp_article.txt
;;
fallback)
# Get title
TITLE=$(curl -s "$ARTICLE_URL" | grep -oP '<title>\K[^<]+' | head -n 1)
TITLE=${TITLE%% - *} # Remove site name
TITLE=${TITLE%% | *} # Remove site name (alternate)
# Get content (basic extraction)
curl -s "$ARTICLE_URL" | python3 -c "
from html.parser import HTMLParser
import sys
class ArticleExtractor(HTMLParser):
def __init__(self):
super().__init__()
self.in_content = False
self.content = []
self.skip_tags = {'script', 'style', 'nav', 'header', 'footer', 'aside', 'form'}
def handle_starttag(self, tag, attrs):
if tag not in self.skip_tags:
if tag in {'p', 'article', 'main'}:
self.in_content = True
if tag in {'h1', 'h2', 'h3'}:
self.content.append('\n')
def handle_data(self, data):
if self.in_content and data.strip():
self.content.append(data.strip())
def get_content(self):
return '\n\n'.join(self.content)
parser = ArticleExtractor()
parser.feed(sys.stdin.read())
print(parser.get_content())
" > temp_article.txt
;;
esac
# Clean filename
FILENAME=$(echo "$TITLE" | tr '/' '-' | tr ':' '-' | tr '?' '' | tr '"' '' | tr '<>' '' | tr '|' '-' | cut -c 1-80 | sed 's/ *$//' | sed 's/^ *//')
FILENAME="${FILENAME}.txt"
# Move to final filename
mv temp_article.txt "$FILENAME"
# Show result
echo "✓ Extracted article: $TITLE"
echo "✓ Saved to: $FILENAME"
echo ""
echo "Preview (first 10 lines):"
head -n 10 "$FILENAME"
错误处理
常见问题
1. 未安装工具
- 尝试其他工具(reader → trafilatura → 备用方案)
- 提供安装方法:“使用以下命令安装 reader:npm install -g reader-cli”
2. 需要付费订阅或登录
- 提取工具可能会失败
- 告知用户:“这篇文章需要身份验证,无法提取。”
3. URL 无效
- 检查 URL 格式
- 尝试使用和不使用重定向
4. 未提取到内容
- 网站可能大量使用 JavaScript
- 尝试备用方法
- 如果提取失败,告知用户
5. 标题中包含特殊字符
- 清理标题以便用于文件系统
- 移除:
/、:、?、"、<、>、| - 替换为
-或直接删除
输出格式
保存的文件包含:
- 文章标题(如果有)
- 作者(如果工具能获取)
- 文章正文
- 章节标题
- 不包含导航、广告或杂项
将移除的内容:
- 导航菜单
- 广告和推广内容
- 订阅新闻简报表单
- 相关文章侧栏
- 评论部分(可选)
- 社交媒体按钮
- Cookie 提示
获得最佳结果的提示
1. 大多数文章使用 reader
- 综合表现最佳的工具
- 基于 Firefox 阅读模式
- 适用于大多数新闻网站和博客
2. 以下内容使用 trafilatura:
- 学术文章
- 新闻网站
- 布局复杂的博客
- 非英语内容
3. 备用方法的局限性:
- 可能会包含一些噪声
- 段落识别准确度较低
- 对简单网站来说,有总比没有好
4. 检查提取质量:
- 始终向用户展示预览
- 询问内容是否正确
- 如有需要,提供尝试其他工具的选项
使用示例
简单提取:
# User: "Extract https://example.com/article"
reader "https://example.com/article" > temp.txt
TITLE=$(head -n 1 temp.txt | sed 's/^# //')
FILENAME="$(echo "$TITLE" | tr '/' '-').txt"
mv temp.txt "$FILENAME"
echo "✓ Saved to: $FILENAME"
带错误处理:
if ! reader "$URL" > temp.txt 2>/dev/null; then
if command -v trafilatura &> /dev/null; then
trafilatura --URL "$URL" --output-format txt > temp.txt
else
echo "Error: Could not extract article. Install reader or trafilatura."
exit 1
fi
fi
最佳实践
- ✅ 提取后始终显示预览(前 10 行)
- ✅ 保存前确认提取成功
- ✅ 清理文件名,确保与文件系统兼容
- ✅ 如果主要方法失败,尝试备用方法
- ✅ 告知用户使用了哪个工具
- ✅ 文件名长度保持合理(< 100 个字符)
提取后
向用户显示:
- “✓ 已提取:[文章标题]”
- “✓ 已保存到:[文件名]”
- 显示预览(前 10-15 行)
- 文件大小和位置
如有需要,询问:
- “需要我基于此创建 Ship-Learn-Next 计划吗?”(如果使用 ship-learn-next skill)
- “还要提取另一篇文章吗?”
评论
支持 Markdown