基于 Model Context Protocol (MCP) 的 TypeScript 网页爬取服务器,内置多种常用爬取与命令工具,能高效完成各类网页内容的爬取任务。
本项目是基于 Model Context Protocol (MCP) 的 TypeScript 网页爬取服务器。它内置了多种常用的爬取与命令工具,助您轻松完成网页内容的爬取与处理。
# 克隆项目
git clone
cd web-scraper-mcp
# 安装依赖
npm install
# 构建项目
npm run build
npm start
# 克隆项目
git clone
cd web-scraper-mcp
# 安装依赖
npm install
# 构建项目
npm run build
npm start
{
"name": "scrape_url",
"arguments": {
"url": "https://example.com",
"format": "markdown",
"usePuppeteer": false,
"ruleSet": "blog",
"customHeaders": {
"Authorization": "Bearer token"
}
}
}
参数说明:
url(必需):要爬取的网页 URL。format(可选):导出格式,支持 markdown、text、html、json,默认为 markdown。usePuppeteer(可选):是否使用 Puppeteer 无头浏览器,默认为 false。ruleSet(可选):规则集名称,用于提取特定内容。customHeaders(可选):自定义请求头。{
"name": "create_rule_set",
"arguments": {
"name": "my_blog_rule",
"rules": {
"title": "h1, .post-title",
"content": ".post-content, article",
"links": ".post-content a",
"images": ".post-content img",
"exclude": ["nav", "footer", ".sidebar"]
}
}
}
参数说明:
name(必需):规则集名称。rules(必需):提取规则配置。
title:标题选择器。content:内容选择器。links:链接选择器。images:图片选择器。exclude:要排除的选择器列表。{
"name": "set_domain_headers",
"arguments": {
"domain": "example.com",
"headers": {
"Authorization": "Bearer your-token",
"X-Custom-Header": "custom-value"
}
}
}
{
"name": "batch_scrape",
"arguments": {
"urls": [
"https://example1.com",
"https://example2.com",
"https://example3.com"
],
"format": "markdown",
"usePuppeteer": false,
"ruleSet": "blog"
}
}
系统内置了以下规则集:
将网页内容转换为 Markdown 格式,包含标题、内容、链接和图片。
纯文本格式,去除所有 HTML 标签。
清理后的 HTML 格式,移除脚本和样式。
完整的结构化数据,包含所有提取的信息。
src/
├── index.ts # 主服务器文件
├── scraper/
│ └── webScraper.ts # 网页爬取器
├── export/
│ └── exportManager.ts # 导出管理器
├── rules/
│ └── ruleEngine.ts # 规则引擎
├── headers/
│ └── headerManager.ts # 请求头管理器
└── utils/
└── logger.ts # 日志工具
# 监听文件变化并自动重新构建
npm run dev
npm test
LOG_LEVEL:日志级别 (debug, info, warn, error)。PUPPETEER_TIMEOUT:Puppeteer 超时时间(毫秒)。REQUEST_TIMEOUT:HTTP 请求超时时间(毫秒)。可以通过修改源代码来自定义:
src/headers/headerManager.ts。src/rules/ruleEngine.ts。src/export/exportManager.ts。A:可能是因为网站有反爬虫机制。尝试:
usePuppeteer: true 启用无头浏览器。A:使用 set_domain_headers 工具设置包含认证信息的请求头,如 Cookie 或 Authorization。
A:设置 usePuppeteer: true 使用 Puppeteer 渲染动态内容。
MIT License
欢迎提交 Issue 和 Pull Request!