# 步骤 1:准备工具和API
- 注册 Google Custom Search JSON API:
- 前往Google Cloud Console (opens new window)创建项目。
- 创建搜索引擎ID(控制台 (opens new window),允许搜索全网,记为
GOOGLE_CSE_ID
)。 - 启用 Custom Search API (opens new window)。
- 获取API密钥(记为
GOOGLE_API_KEY
)。
# 步骤 2:请求获取数据
const keyword = '我的后半生 夸克网盘'
const GOOGLE_API_KEY = '****';
const GOOGLE_CSE_ID = '***';
const apiUrl = `https://www.googleapis.com/customsearch/v1?key=${GOOGLE_API_KEY}&cx=${GOOGLE_CSE_ID}&q=${encodeURIComponent(keyword)}`;
fetch(apiUrl);