You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中用SerpAPI抓取谷歌赞助站点失败,请求技术排查

问题分析与解决方案

你的代码存在几个关键问题,导致无法获取赞助结果,并非API不支持抓取链接:

1. 变量名大小写与引用错误

Python是大小写敏感语言,你定义了Sponsored_results,但后续判断时用了sponsored_results,且循环中引用了未定义的results变量,同时输出时误用了键名Sponsored而非循环变量ad。

2. 引擎选择错误

google_local是谷歌本地商家搜索引擎,其返回结构中没有Sponsored或shopping_results字段,普通赞助广告需要使用标准谷歌搜索引擎(engine="google")。

3. 参数冲突

location="Indonesia"和gl="us"(代表美国地区)存在冲突,会导致搜索结果地区不匹配,建议统一为印尼地区参数。


修正后的代码

from serpapi import GoogleSearch
import json

params = {
    "api_key": "APIKEY",
    "engine": "google",  # 更换为标准谷歌搜索引擎
    "q": "Crypto Buy",
    "location": "Indonesia",
    "google_domain": "google.co.id",
    "gl": "id",  # 统一为印尼地区代码
    "hl": "en"
}

search = GoogleSearch(params)
results = search.get_dict()  # 统一变量名

# 检查标准赞助广告字段
if results.get("sponsored_results", []):
    for ad in results["sponsored_results"]:
        print(json.dumps(ad, indent=2))

# 检查购物类赞助广告字段
if results.get("shopping_results", []):
    for shopping_ad in results["shopping_results"]:
        print(json.dumps(shopping_ad, indent=2))

# 若两类都为空则提示
if not results.get("sponsored_results", []) and not results.get("shopping_results", []):
    print("no sponsors found.")

额外说明

  • 标准谷歌搜索引擎的赞助广告字段为sponsored_results(注意小写开头),而非你代码中的Sponsored。
  • 若仍无结果,可能是当前搜索词+地区组合下谷歌确实没有投放赞助广告,可以更换搜索词(如"Crypto Exchange Indonesia")或地区测试。

内容的提问来源于stack exchange,提问作者CyberHunter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 17:53:28