网页抓取转换工具

一个命令行工具和MCP服务器,用于抓取网页并将HTML转换为Markdown格式,支持本地文件转换和安全处理HTML。

  • by tolik-unicornrider
  • JavaScript
  • 本地部署
  • 8 星标
  • 更新于
在 GitHub 查看 tolik-unicornrider/mcp_scraper

使用说明

网站爬虫

一个命令行工具和MCP服务器,用于抓取网站并将HTML转换为Markdown。

特性

  • 使用Mozilla从网页中提取有意义的内容 可读性 库(与Firefox阅读器视图中使用的引擎相同)
  • 使用TurndownService将干净的HTML转换为高质量的Markdown
  • 通过删除可能有害的脚本标签来安全地处理HTML
  • 既可作为命令行工具,又可作为MCP服务器
  • 支持将本地HTML文件直接转换为Markdown

安装

# Install dependencies
npm install

# Build the project
npm run build

# Optionally, install globally
npm install -g .

用法

命令行接口命令模式

# Print output to console
scrape https://example.com

# Save output to a file
scrape https://example.com output.md

# Convert a local HTML file to Markdown
scrape --html-file input.html

# Convert a local HTML file and save output to a file
scrape --html-file input.html output.md

# Show help
scrape --help

# Or run via npm script
npm run start:cli -- https://example.com

MCP服务器模式

此工具可用作模型上下文协议(MCP)服务器:

# Start in MCP server mode
npm start

编码结构

  • src/index.ts -核心功能和MCP服务器实现
  • src/cli.ts -命令行界面实现
  • src/data_processing.ts -HTML到Markdown转换功能

API

该工具导出以下功能:

// Scrape a website and convert to Markdown
import { scrapeToMarkdown } from './build/index.js';

// Convert HTML string to Markdown directly
import { htmlToMarkdown } from './build/data_processing.js';

async function example() {
  // Web scraping
  const markdown = await scrapeToMarkdown('https://example.com');
  console.log(markdown);
  
  // Direct HTML conversion
  const html = '<h1>Hello World</h1><p>This is <strong>bold</strong> text.</p>';
  const md = htmlToMarkdown(html);
  console.log(md);
}

许可证

国际协调委员会