基于 Playwright 的 Node.js 工具,绕过搜索引擎反爬虫机制执行 Google 搜索并提取结果。可以直接作为命令行工具使用,也可以作为模型上下文协议(MCP)服务器提供实时搜索能力给像 Claude 这样的 AI 助手。
# 从源码安装
git clone https://github.com/web-agent-master/google-search.git
cd google-search
# 安装依赖
npm install
# 或者使用 yarn
yarn
# 或者使用 pnpm
pnpm install
# 编译 TypeScript 代码
npm run build
# 或者使用 yarn
yarn build
# 或者使用 pnpm
pnpm build
# 全局链接包(MCP 功能所需)
npm link
# 或者使用 yarn
yarn link
# 或者使用 pnpm
pnpm link
此工具已特别适应 Windows 环境:
.cmd 文件确保命令行工具在 Windows 命令提示符和 PowerShell 中正常工作。/tmp 目录。# 直接命令行使用
google-search "搜索关键词"
# 使用命令行选项
google-search --limit 5 --timeout 60000 --no-headless "搜索关键词"
# 或者使用 npx
npx google-search-cli "搜索关键词"
# 在开发模式下运行
pnpm dev "搜索关键词"
# 在调试模式下运行(显示浏览器界面)
pnpm debug "搜索关键词"
# 获取搜索结果页面的原始 HTML
google-search "搜索关键词" --get-html
# 获取 HTML 并保存到文件
google-search "搜索关键词" --get-html --save-html
# 获取 HTML 并保存到指定文件
google-search "搜索关键词" --get-html --save-html --html-output "./output.html"
-l, --limit <number>:结果数量限制(默认:10)-t, --timeout <number>:超时时间(毫秒,默认:60000)--no-headless:显示浏览器界面(用于调试)--remote-debugging-port <number>:启用远程调试端口(默认:9222)--state-file <path>:浏览器状态文件路径(默认:./browser-state.json)--no-save-state:不保存浏览器状态--get-html:获取搜索结果页面的原始 HTML 而不是解析结果--save-html:将 HTML 保存到文件(与 --get-html 一起使用)--html-output <path>:指定 HTML 输出文件路径(与 --get-html 和 --save-html 一起使用)-V, --version:显示版本号-h, --help:显示帮助信息{
"query": "deepseek",
"results": [
{
"title": "DeepSeek",
"link": "https://www.deepseek.com/",
"snippet": "DeepSeek-R1 现已上线并开源,与 OpenAI 的 Model o1 相媲美。可在网页、应用和 API 上使用。点击查看详情。深入..."
},
{
"title": "DeepSeek",
"link": "https://www.deepseek.com/",
"snippet": "DeepSeek-R1 现已上线并开源,与 OpenAI 的 Model o1 相媲美。可在网页、应用和 API 上使用。点击查看详情。深入..."
},
{
"title": "deepseek-ai/DeepSeek-V3",
"link": "https://github.com/deepseek-ai/DeepSeek-V3",
"snippet": "我们推出了 DeepSeek-V3,这是一个强大的混合专家(MoE)语言模型,总参数量为 671B,每个令牌激活 37B。"
}
// 更多结果...
]
}
使用 --get-html 选项时,输出将包括关于 HTML 内容的信息:
{
"query": "playwright automation",
"url": "https://www.google.com/",
"originalHtmlLength": 1291733,
"cleanedHtmlLength": 456789,
"htmlPreview": "<!DOCTYPE html><html itemscope=\"\" itemtype=\"http://schema.org/SearchResultsPage\" lang=\"zh-CN\"><head><meta charset=\"UTF-8\"><meta content=\"dark light\" name=\"color-scheme\"><meta content=\"origin\" name=\"referrer\">..."
}
如果还使用了 --save-html 选项,输出将包括 HTML 保存的路径:
{
"query": "playwright automation",
"url": "https://www.google.com/",
"originalHtmlLength": 1292241,
"cleanedHtmlLength": 458976,
"savedPath": "./google-search-html/playwright_automation-2025-04-06T03-30-06-852Z.html",
"screenshotPath": "./google-search-html/playwright_automation-2025-04-06T03-30-06-852Z.png",
"htmlPreview": "<!DOCTYPE html><html itemscope=\"\" itemtype=\"http://schema.org/SearchResultsPage\" lang=\"zh-CN\">..."
}
该项目提供了模型上下文协议(MCP)服务器功能,允许像 Claude 这样的 AI 助手直接使用 Google 搜索功能。MCP 是一个开放协议,使 AI 助手能够安全地访问外部工具和数据。
# 构建项目
pnpm build
编辑 Claude Desktop 配置文件:
~/Library/Application Support/Claude/claude_desktop_config.json%APPDATA%\Claude\claude_desktop_config.json
C:\Users\username\AppData\Roaming\Claude\claude_desktop_config.json%APPDATA%\Claude 直接访问它添加服务器配置并重启 Claude
{
"mcpServers": {
"google-search": {
"command": "npx",
"args": ["google-search-mcp"]
}
}
}
对于 Windows 环境,还可以使用以下配置:
{
"mcpServers": {
"google-search": {
"command": "cmd.exe",
"args": ["/c", "npx", "google-search-mcp"]
}
}
}
{
"mcpServers": {
"google-search": {
"command": "node",
"args": ["C:/path/to/your/google-search/dist/src/mcp-server.js"]
}
}
}
注意:对于第二种方法,必须将 C:/path/to/your/google-search 替换为你实际安装 google-search 包的完整路径。
集成后,你可以在 Claude 中直接使用搜索功能,例如“搜索最新的 AI 研究”。
google-search/
├── package.json # 项目配置和依赖项
├── tsconfig.json # TypeScript 配置
├── src/
│ ├── index.ts # 入口文件(命令行解析和主要逻辑)
│ ├── search.ts # 搜索功能实现(Playwright 浏览器自动化)
│ ├── mcp-server.ts # MCP 服务器实现
│ └── types.ts # 类型定义(接口和类型声明)
├── dist/ # 编译后的 JavaScript 文件
├── bin/ # 可执行文件
│ └── google-search # 命令行入口脚本
├── README.md # 项目文档
└── .gitignore # Git 忽略文件
所有命令都可以在项目根目录下运行:
# 安装依赖
pnpm install
# 安装 Playwright 浏览器
pnpm run postinstall
# 编译 TypeScript 代码
pnpm build
# 清理编译输出
pnpm clean
# 在开发模式下运行
pnpm dev "搜索关键词"
# 在调试模式下运行(显示浏览器界面)
pnpm debug "搜索关键词"
# 运行编译后的代码
pnpm start "搜索关键词"
# 测试搜索功能
pnpm test
# 在开发模式下运行 MCP 服务器
pnpm mcp
# 运行编译后的 MCP 服务器
pnpm mcp:build
该工具内置了强大的错误处理机制:
.google-search-browser-state.json。google-search-logs 文件夹中。与付费的搜索引擎结果 API 服务(如 SerpAPI)相比,本项目具有以下优势: