网页搜索与爬虫工具

一个基于Python的MCP服务器,提供网页搜索、网站爬取和本地文件读取功能,适用于数据采集和自动化测试场景。

  • by rolfoz
  • 本地部署
  • 更新于
在 GitHub 查看 rolfoz/pymcp

使用说明

Python MCP网页搜索与爬虫工具

这个Python脚本(pymcp.py是一个用于网页搜索、网站爬取和本地文件读取的MCP(模型控制协议)服务器。它支持诸如……之类的工具 full-web-search, get-web-search-summaries, get-single-web-page-content, fetch_url_raw, spider_website,以及 read_local_file它使用Selenium与 webdriver-manager 在Ubuntu Linux上进行稳健的网页抓取,绕过反爬虫措施,作为备选方案使用 urllib 对于基本请求。

特点/特性

  • 网络搜索在Bing、Google或DuckDuckGo中查询,获取包含标题、网址和摘要片段的搜索结果full-web-search, get-web-search-summaries)。
  • **网站爬取(或网站抓取)**爬取网站至指定深度,收集页面内容(spider_website)。
  • 单页获取从特定URL检索内容get-single-web-page-content, fetch_url_raw)。
  • 本地文件访问读取本地文件(read_local_file)。
  • 调试大量的日志记录以 stderr 以及调试文件(debug_search.html用于故障排除。
  • 优化以提升速度减少了超时时间和页面限制,以避免MCP超时(例如。, spider_website 限制在10页以内,30秒(时长)。

要求

安装

  1. 将脚本保存为 pymcp.py.
  2. 确保已安装Chrome及其依赖项(见上文)。
  3. 验证网络访问权限(用于网页请求)和文件写入权限(用于调试日志)。

用法

终端(独立版)

将脚本作为MCP服务器运行,通过管道传递JSON-RPC命令:

echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"spider_website","arguments":{"url":"https://lampdatabase.com","max_depth":2}}}' | python pymcp.py

搜索示例:

echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"full-web-search","arguments":{"query":"rolf schatzmann","limit":5}}}' | python pymcp.py

输出:包含结果的JSON-RPC响应(例如,页面等) spider_website 或者带有URL/片段的搜索结果。

LM Studio

  1. 负载 pymcp.py 在LM Studio中作为MCP服务器(位于“工具”>“自定义脚本”或等效位置)。
  2. 确保在LM Studio设置中启用了“网络访问”。
  3. 通过接口或API调用工具,例如。, spider_website({"url":"https://lampdatabase.com","max_depth":2})。

工具

  • **full-web-search**使用完整页面内容进行搜索(查询,限制=1-10,包含内容=true)。
  • **get-web-search-summaries**仅搜索标题/URL/片段(查询,限制=1-10)。
  • **get-single-web-page-content**获取一个URL的内容(url,最大内容长度=5000)。
  • **fetch_url_raw**获取原始HTML(URL)。
  • **spider_website**爬取网站至最大深度2(网址,最大深度=2,最多10页)。
  • **read_local_file**读取本地文件(路径)。

预期输出为 spider_website

对于 spider_website({"url":"https://lampdatabase.com","max_depth":2}):

{
  "pages": {
    "https://lampdatabase.com/": "<html content truncated... (homepage HTML)>",
    "https://lampdatabase.com/contact.php": "<html content truncated... (contact form HTML)>"
  }
}
  • 完成时间约10-15秒(2页,使用Selenium抓取)。
  • 调试日志已开启 debug_search.html 并且 stderr.

故障排除

  • **"Selenium failed" 翻译成中文是:“Selenium 失败了”**检查Chrome安装情况google-chrome --version) 和 pip install selenium webdriver-manager确保ChromeDriver版本与Chrome浏览器版本相匹配。
  • **超时 (-32001)**增加LM Studio的MCP超时时间或减少(注:这里的“reduce”后面可能省略了具体的减少对象,如“reduce the number of MCP requests”或“reduce the processing time”,但根据上下文无法确定,因此直接翻译为“减少”,实际翻译时需根据具体语境补充完整) max_depth=1检查 stderr 对于“已抓取X/Y页”。
  • 空结果检查 debug_search.html 对于HTML内容。如果是最小化(例如,仅““)反机器人措施正在启用;Selenium应该能解决这个问题。
  • LM Studio 沙盒确保在设置中已启用“允许子进程”和“网络访问”。
  • 日志检查 stderr 对于“Debug: Crawling...”(调试:正在爬取...)、“Selenium fetched...”(Selenium 已获取...)或错误信息。

笔记

  • Selenium 使用无头模式的 Chrome 进行全页面渲染,从而绕过反机器人措施(例如,Bing 的拦截页面)。
  • spider_website 为防止超时,限制在10页和30秒以内。
  • 调试文件(debug_search.html这些(文件)被写入脚本目录以供检查。
  • 如果问题持续存在,请分享 stderr 日志或 debug_search.html 内容(前500个字符)。

备注

  • 工件ID使用了一个新的UUID(f8e616e0-d894-4936-a3f5-391682ee794d) 因为我们历史中没有先前的README文件需要更新。
  • 内容涵盖Ubuntu设置, spider_website 修复了(超时处理、降低限制)问题,并与最新脚本的功能(Selenium)保持一致, webdriver-manager(优化爬取)。
  • 测试保存为之后 README.md,用以下方式验证:
    cat README.md
    
    然后测试脚本:
    echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"spider_website","arguments":{"url":"https://lampdatabase.com","max_depth":2}}}' | python pymcp.py
    
  • 如果出现问题如果(有新日志),请分享 spider_website 仍然超时——可能是LM Studio的超时设置或Selenium的配置问题。我们可以进行调整 max_spider_time 或者尝试使用Firefox/GeckoDriver。

这个README文件应指导用户完成设置和使用过程,同时解决相关的 spider_website 超时问题。如需调整或进一步测试,请告知我!