网页爬虫新闻提取服务

一个基于MCP协议的网页爬虫服务,用于提取新闻文章内容、处理RSS订阅源,并提供结构化数据输出,适用于LLM应用的实时网络访问需求。

  • by saiminhtet
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 saiminhtet/web-crawler-mcp-server

使用说明

Web爬虫MCP服务器

用于网络爬行和新闻文章提取的模型上下文协议(MCP)服务器。此服务器使LLM能够浏览网页,使用newspaper4k提取文章内容,并使用结构化数据输出处理RSS提要。

特性

  • 📰 提取包含元数据(标题、作者、发布日期、内容)的完整新闻文章
  • 🔍 从RSS源中发现和处理内容
  • 🎯 使用newspaper4k库进行智能内容解析
  • ⚡ 具有并发请求的批处理URL
  • 🔄 LLM应用程序的实时web访问
  • 🤖 完全符合MCP SDK标准,实现无缝集成

安装

先决条件

  • Python 3.9或更高版本
  • pip包管理器

再进行

# Clone the repository
cd web-crawler-mcp-server

# Install required packages
pip install -r requirements.txt

# Or using pyproject.toml
pip install -e .

所需套餐

  • mcp>=1.0.0 -模型上下文协议SDK
  • newspaper4k>=0.2.8 -新闻文章摘录
  • beautifulsoup4>=4.12.0 -HTML解析
  • aiohttp>=3.9.0 -异步HTTP客户端
  • feedparser>=6.0.0 -RSS源解析
  • lxml>=4.9.0 -XML处理
  • lxml_html_clean>=0.4.0 -lxml的HTML清理(报纸4k要求)

配置

克劳德桌面

添加到您的Claude Desktop配置文件中:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json

视窗: %APPDATA%\Claude\claude_desktop_config.json

配置(推荐-使用主目录快捷方式):

{
  "mcpServers": {
    "web-crawler": {
      "command": "/Users/saiminhtet/web-crawler-mcp-server/venv/bin/python",
      "args": ["-m", "web_crawler_mcp.server"]
    }
  }
}

替代配置(通用模板):

{
  "mcpServers": {
    "web-crawler": {
      "command": "/absolute/path/to/web-crawler-mcp-server/venv/bin/python",
      "args": ["-m", "web_crawler_mcp.server"]
    }
  }
}

重要提示:

  • 使用虚拟环境的Python(in venv/bin/python)不是系统Python
  • 模块名称为 web_crawler_mcp.server (没有 src. 安装时使用前缀)
  • 使用 绝对路径 (不是 ~ 或相对路径)
  • 如果路径包含空格或特殊字符,请创建符号链接: ln -sf "/path/with/spaces" ~/web-crawler-mcp-server
  • 在Windows上,使用 venv\Scripts\python.exe 而不是 venv/bin/python
  • 更新配置后,重新启动Claude Desktop

Windows示例:

{
  "mcpServers": {
    "web-crawler": {
      "command": "C:\\Users\\YourName\\web-crawler-mcp-server\\venv\\Scripts\\python.exe",
      "args": ["-m", "web_crawler_mcp.server"]
    }
  }
}

解决路径问题:

如果您在路径中遇到带有特殊字符的JSON解析错误:

# Create a symlink without special characters
ln -sf "/Users/saiminhtet/Research&Development/python/ai-development/web-crawler-mcp-server" ~/web-crawler-mcp-server

# Then use the symlink path in your config:
# /Users/saiminhtet/web-crawler-mcp-server/venv/bin/python

设置步骤

  1. 创建并激活虚拟环境:
cd /path/to/web-crawler-mcp-server
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. 安装软件包:
pip install -e .
  1. 更新Claude桌面配置 (见上面的配置)

  2. 重新启动克劳德桌面

其他MCP客户端

直接从虚拟环境运行服务器:

# Activate virtual environment first
source venv/bin/activate

# Run the server
python -m web_crawler_mcp.server

服务器通过stdio进行通信,并遵循MCP协议规范。

可用工具

1. crawl_news_article

提取并解析一篇包含完整元数据的新闻文章。

参数:

  • url (string,必填):要抓取的新闻文章的URL
  • language (字符串,可选):文章的语言(默认值:“en”)

退货: JSON,包含文章详细信息,包括标题、文本、摘要、关键字、作者、发布日期、图像和元数据。

例子:

{
  "url": "https://example.com/article",
  "language": "en"
}

2. extract_multiple_news_articles

通过批处理一次提取多篇新闻文章。

参数:

  • urls (字符串数组,必填):要爬网的URL列表
  • language (字符串,可选):文章的语言(默认值:“en”)

退货: 包含所有文章结果的JSON数组。

例子:

{
  "urls": [
    "https://example.com/article1",
    "https://example.com/article2"
  ],
  "language": "en"
}

3. discover_news_from_rss

从RSS提要中发现和提取新闻文章。

参数:

  • rss_url (字符串,必填):RSS源的URL
  • max_articles (整数,可选):要提取的最大文章数(默认值:10)

退货: 包含RSS元数据和完整内容的JSON文章数组。

例子:

{
  "rss_url": "http://rss.cnn.com/rss/edition.rss",
  "max_articles": 5
}

4. search_and_extract_news

在多个RSS提要中搜索新闻并提取匹配的内容。

参数:

  • query (string,必填):在新闻文章中查找的搜索查询
  • rss_feeds (字符串数组,可选):要搜索的RSS提要URL列表
  • max_results (整数,可选):返回的最大结果数(默认值:5)

退货: 包含完整内容的匹配文章的JSON数组。

例子:

{
  "query": "artificial intelligence",
  "rss_feeds": [
    "http://rss.cnn.com/rss/edition.rss",
    "https://feeds.bbci.co.uk/news/rss.xml"
  ],
  "max_results": 5
}

5. get_news_summary

获取一篇包含关键点的新闻文章的摘要版本。

参数:

  • url (string,必填):新闻文章的URL
  • summary_length (整数,可选):摘要的句子数(默认值:5)

退货: JSON,包含文章摘要、标题、关键字、作者和关键元数据。

例子:

{
  "url": "https://example.com/article",
  "summary_length": 5
}

用法示例

配置后,您可以通过MCP客户端使用服务器:

"Crawl the latest news from https://example.com/article and summarize it"

"Extract articles from the CNN RSS feed and find stories about technology"

"Get summaries of these 3 articles: [URL1, URL2, URL3]"

"Search for AI-related news across BBC and Reuters RSS feeds"

响应格式

所有工具都返回具有一致结构的JSON响应:

成功响应:

{
  "url": "https://example.com/article",
  "title": "Article Title",
  "text": "Full article text...",
  "summary": "Article summary...",
  "keywords": ["keyword1", "keyword2"],
  "authors": ["Author Name"],
  "publish_date": "2025-01-15",
  "top_image": "https://example.com/image.jpg",
  "images": ["image1.jpg", "image2.jpg"],
  "meta_description": "Article description",
  "success": true
}

错误响应:

{
  "url": "https://example.com/article",
  "success": false,
  "error": "Error description"
}

用于测试的常见RSS源

  • 美国有线电视新闻网: http://rss.cnn.com/rss/edition.rss
  • 英国广播公司新闻网: https://feeds.bbci.co.uk/news/rss.xml
  • 《纽约时报》: https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml
  • 路透社商业: https://www.reutersagency.com/feed/?best-topics=商业金融&职位类型=最佳

最佳实践

速率限制

服务器在请求之间实现延迟(1秒),以尊重网站。调整 asyncio.sleep() 值在 tools.py 根据需要。

Robots.txt

在抓取网站时,始终遵守robots.txt指令。

错误处理

所有工具都包括针对网络问题、解析错误和无效URL的强大错误处理。

内存管理

服务器按顺序处理文章以管理内存使用情况。对于大批量操作,考虑实现分页。

用户代理

服务器使用标准浏览器用户代理来避免被阻止:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36

发展

项目结构

web-crawler-mcp-server/
├── src/
│   └── web_crawler_mcp/
│       ├── __init__.py
│       ├── server.py        # MCP server implementation
│       └── tools.py          # Web crawler tools
├── requirements.txt
├── pyproject.toml
└── README.md

运行测试

# Test individual article crawling
python -c "
import asyncio
from src.web_crawler_mcp.tools import WebCrawlerTools

async def test():
    tools = WebCrawlerTools()
    await tools.init_session()
    result = await tools.crawl_news_article('https://example.com/article')
    print(result)
    await tools.close_session()

asyncio.run(test())
"

故障排除

导入错误

确保安装了所有依赖项: pip install -r requirements.txt

连接超时

调整 request_timeout 在 tools.py WebCrawlerTools初始化(默认值:10秒)

文章解析失败

一些网站可能会阻止自动爬虫。尝试调整用户代理或使用其他来源。

RSS源错误

验证RSS源URL是否有效且可访问。某些订阅源可能需要身份验证。

许可证

MIT许可证-有关详细信息,请参阅许可证文件

贡献

欢迎投稿!请提交pull请求或bug和功能请求的开放问题。

作者

赛敏泰(saiminhtet@u.nus.edu)

致谢