关于 spider
spider 是使用命令行工具和 Gemini 抓取、分析网站页面的技能,适合需要检查页面、提取数据或追踪网站变化的用户。
主要功能
- 01使用 curl 获取页面
- 02提取并筛选页面链接
- 03抓取多个页面和站点地图
- 04使用 Gemini 分析内容
- 05提取文本、结构化数据和 API 端点
- 06通过哈希比较页面变化
适用场景
- 01扫描网站并检查响应头
- 02从网页提取产品数据
- 03查找 JavaScript 中的 API 端点
- 04检查页面是否发生变化
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ johnlindquist/ claude --skill spider安装后在 Claude Code 中输入/spider确认已加载。
详细介绍
网页爬虫
爬取和分析网站。
前提条件
# curl for fetching
curl --version
# Gemini for analysis
pip install google-generativeai
export GEMINI_API_KEY=your_api_key
# Optional: better HTML parsing
npm install -g puppeteer
pip install beautifulsoup4
基础爬取
获取页面
# Simple fetch
curl -s "https://example.com"
# With headers
curl -s -H "User-Agent: Mozilla/5.0" "https://example.com"
# Follow redirects
curl -sL "https://example.com"
# Save to file
curl -s "https://example.com" -o page.html
# Get headers only
curl -sI "https://example.com"
# Get headers and body
curl -sD - "https://example.com"
提取链接
# Extract all links from a page
curl -s "https://example.com" | grep -oE 'href="[^"]*"' | sed 's/href="//;s/"$//'
# Filter to specific domain
curl -s "https://example.com" | grep -oE 'href="https?://example\.com[^"]*"'
# Unique links
curl -s "https://example.com" | grep -oE 'href="[^"]*"' | sort -u
快速扫描网站
#!/bin/bash
URL=$1
echo "=== Scanning: $URL ==="
echo ""
echo "### Response Info ###"
curl -sI "$URL" | head -20
echo ""
echo "### Links Found ###"
curl -s "$URL" | grep -oE 'href="[^"]*"' | sort -u | head -20
echo ""
echo "### Scripts ###"
curl -s "$URL" | grep -oE 'src="[^"]*\.js[^"]*"' | sort -u
echo ""
echo "### Meta Tags ###"
curl -s "$URL" | grep -oE '<meta[^>]*>' | head -10
AI 驱动的分析
页面分析
CONTENT=$(curl -s "https://example.com")
gemini -m pro -o text -e "" "Analyze this webpage:
$CONTENT
Provide:
1. What is this page about?
2. Key information/content
3. Navigation structure
4. Technical observations (framework, libraries)
5. SEO observations"
安全扫描
URL="https://example.com"
# Gather information
HEADERS=$(curl -sI "$URL")
CONTENT=$(curl -s "$URL" | head -1000)
gemini -m pro -o text -e "" "Security scan this website:
URL: $URL
HEADERS:
$HEADERS
CONTENT SAMPLE:
$CONTENT
Check for:
1. Missing security headers (CSP, HSTS, X-Frame-Options)
2. Exposed sensitive information
3. Potential vulnerabilities in scripts/forms
4. Cookie security settings
5. HTTPS configuration"
提取结构化数据
CONTENT=$(curl -s "https://example.com/products")
gemini -m pro -o text -e "" "Extract structured data from this page:
$CONTENT
Extract into JSON format:
- Product names
- Prices
- Descriptions
- Any available metadata"
多页面爬取
爬取并分析
#!/bin/bash
BASE_URL=$1
MAX_PAGES=${2:-10}
# Get initial links
LINKS=$(curl -s "$BASE_URL" | grep -oE "href=\"$BASE_URL[^\"]*\"" | sed 's/href="//;s/"$//' | sort -u | head -$MAX_PAGES)
echo "Found $(echo "$LINKS" | wc -l) pages"
for link in $LINKS; do
echo ""
echo "=== $link ==="
TITLE=$(curl -s "$link" | grep -oE '<title>[^<]*</title>' | sed 's/<[^>]*>//g')
echo "Title: $TITLE"
done
站点地图处理
# Fetch and parse sitemap
curl -s "https://example.com/sitemap.xml" | grep -oE '<loc>[^<]*</loc>' | sed 's/<[^>]*>//g'
# Crawl pages from sitemap
curl -s "https://example.com/sitemap.xml" | \
grep -oE '<loc>[^<]*</loc>' | \
sed 's/<[^>]*>//g' | \
while read url; do
echo "Processing: $url"
# Your processing here
done
特定内容提取
提取文本内容
# Remove HTML tags (basic)
curl -s "https://example.com" | sed 's/<[^>]*>//g' | tr -s ' \n'
# Using python
curl -s "https://example.com" | python3 -c "
from html.parser import HTMLParser
import sys
class TextExtractor(HTMLParser):
def __init__(self):
super().__init__()
self.text = []
def handle_data(self, data):
self.text.append(data.strip())
p = TextExtractor()
p.feed(sys.stdin.read())
print(' '.join(filter(None, p.text)))
"
提取 API 端点
CONTENT=$(curl -s "https://example.com/app.js")
# Find API calls in JS
echo "$CONTENT" | grep -oE "(fetch|axios|http)\(['\"][^'\"]*['\"]" | sort -u
# AI extraction
gemini -m pro -o text -e "" "Extract API endpoints from this JavaScript:
$CONTENT
List all:
- API URLs
- HTTP methods used
- Request patterns"
监控更改
#!/bin/bash
URL=$1
HASH_FILE="/tmp/page-hash-$(echo $URL | md5sum | cut -d' ' -f1)"
CURRENT=$(curl -s "$URL" | md5sum | cut -d' ' -f1)
if [ -f "$HASH_FILE" ]; then
PREVIOUS=$(cat "$HASH_FILE")
if [ "$CURRENT" != "$PREVIOUS" ]; then
echo "Page changed: $URL"
# Notify or log
fi
fi
echo "$CURRENT" > "$HASH_FILE"
最佳实践
- 遵守 robots.txt - 爬取前先检查
- 限制请求速率 - 不要给服务器造成过大负担
- 设置 User-Agent - 标识你的爬虫
- 处理错误 - 网站可能宕机,页面可能返回 404
- 缓存响应 - 不要不必要地重复获取
- 遵守道德规范 - 只爬取获准爬取的内容
- 查看服务条款 - 有些网站禁止抓取
评论
支持 Markdown