从 Google Search API 获取正确的结果,关键大多在于参数。以下是完整参考,使用 serp.cheap。
所有参数
| 参数 | 类型 | 默认值 | 作用 |
|---|---|---|---|
q | string | 必填 | 搜索查询 |
gl | string | "us" | 结果所属国家(地理位置) |
hl | string | 国家默认值 | 显示/界面语言 |
page | int | 1 | 结果页(从 1 开始) |
tbs | string | 无 | 时间过滤(qdr:h/qdr:d/qdr:w) |
gl——国家(你最常用的参数)
gl 决定你访问哪个 Google以及获取哪个地区的排名。支持:us、br、gb、de、fr、es、it、mx、ca、au、jp、nl。
-d '{"q": "beste elektroautos", "gl": "de"}' # Germany
-d '{"q": "melhores carros elétricos", "gl": "br"}' # Brazil
hl——语言覆盖
gl 已经设定了一个合理的默认语言。仅当你希望获取与该国家默认语言不同的结果时,才使用 hl:
-d '{"q": "best laptops", "gl": "ca", "hl": "fr"}' # Canada, in French
tbs——时间过滤
限定为近期结果——非常适合新闻、监控和热门话题:
-d '{"q": "ai news", "gl": "us", "tbs": "qdr:d"}' # past 24 hours
| 值 | 时间窗口 |
|---|---|
qdr:h | 过去一小时 |
qdr:d | 过去一天 |
qdr:w | 过去一周 |
page——分页
从 1 开始计数;每一页都是单独计费的请求。参见获取 100 条结果。
组合使用
curl https://api.serp.cheap/v1/search \
-H "X-API-Key: YOUR_KEY" -H "Content-Type: application/json" \
-d '{"q": "mortgage rates", "gl": "us", "hl": "en", "page": 1, "tbs": "qdr:w"}'