如何使用 SERP API 采集 Google Maps 结果

了解如何使用 SERP API 和 Python 采集 Google Maps 结果,提取商家名称、评分、评论数、地址、电话、网站、place ID 和 GPS 坐标,并导出为 CSV。

如何使用 SERP API 采集 Google Maps 结果
Ethan Caldwell
最后更新于
6 分钟阅读

Google Maps results 对需要 local business data 的搜索场景非常有用。

例如,你可能想采集 New York 的餐厅、London 的牙医、机场附近的酒店、某个城市的维修店,或查看某个 local search result 中出现了哪些 competitors。

一条 Google Maps SERP result 通常可以包含:

  • business name

  • rating

  • review count

  • address

  • phone number

  • website

  • business type

  • opening hours

  • place ID

  • GPS coordinates

  • result position

你可以尝试直接用 browser automation 抓取 Google Maps,但通常需要处理 page rendering、scrolling、dynamic loading、anti-bot checks、proxy rotation 和频繁变化的 layout。

SERP API 通常是更简单的方式。你只需要发送 query 和 location,就可以收到 structured JSON results。

这篇文章会用 Python 示范如何采集 Google Maps results,整理数据,并导出为 CSV。

为什么要采集 Google Maps Results?

Google Maps data 适合很多 workflow:

  • Local SEO:追踪哪些商家在 local keywords 下排名。

  • Lead generation:采集 business names、websites 和 contact information。

  • Market research:比较不同城市的 local competitors。

  • Reputation monitoring:追踪 ratings 和 review counts。

  • AI agents:让 agent 在生成推荐前获取最新 local business data。

  • Dashboards:为 agency 或内部团队建立报告。

例如 local SEO agency 可能会搜索:

dentist in Austin
plumber near Chicago
best coffee shop in Seattle
law firm in Boston

然后比较哪些 businesses 出现、排名第几,以及 ratings 是否随时间改变。

为什么使用 SERP API?

手动抓取 Google Maps 很容易变得脆弱。

你可能需要处理:

  • browser rendering

  • infinite scroll

  • dynamic JavaScript

  • blocked requests

  • CAPTCHA challenges

  • changing HTML structure

  • location-specific results

  • mobile vs desktop differences

SERP API 会帮你处理大部分 collection layer。

使用 SERP API 后,workflow 会变得更简单:

Send query + location
→ Receive structured JSON
→ Extract business fields
→ Save to CSV, database, or dashboard

Talordata、SerpApi、DataForSEO、Bright Data、SearchAPI.io 和 Serper.dev 都可以用于这类 workflow。下面的 code 是 provider-neutral 的,你可以根据自己使用的 API 调整。查看API文档

我们要做什么?

我们会建立一个小型 Python script,用来:

  1. 向 SERP API 发送 Google Maps-style search query

  2. 读取 JSON response

  3. 提取 local business results

  4. normalize fields

  5. 将结果导出为 CSV file

先安装需要的 package:

pip install requests

Python 内置的 csv module 已经足够导出数据,所以不需要额外安装 CSV library。

Step 1:设置 API Key

建议使用 environment variables,不要把 credentials 直接写在代码里。

export SERP_API_KEY="your_api_key_here"
export SERP_API_ENDPOINT="https://your-serp-api-endpoint.com/search"

Windows PowerShell:

$env:SERP_API_KEY="your_api_key_here"
$env:SERP_API_ENDPOINT="https://your-serp-api-endpoint.com/search"

不同 provider 的 endpoint format 可能不同。有些使用 q,有些使用 query。有些使用 engine=google_maps,有些使用 type=maps 或 dedicated Maps endpoint。

核心思路是一样的。

Step 2:请求 Google Maps Results

下面是一个 basic request function:

import os
import requests


SERP_API_KEY = os.getenv("SERP_API_KEY")
SERP_API_ENDPOINT = os.getenv("SERP_API_ENDPOINT")


def search_google_maps(query, location="United States"):
    params = {
        "engine": "google_maps",
        "query": query,
        "location": location,
        "output": "json"
    }

    headers = {
        "Authorization": f"Bearer {SERP_API_KEY}"
    }

    response = requests.get(
        SERP_API_ENDPOINT,
        params=params,
        headers=headers,
        timeout=30
    )

    response.raise_for_status()
    return response.json()

根据你的 SERP API provider,可能需要把:

"engine": "google_maps"

改成:

"type": "maps"

或:

"search_type": "local"

如果你的 provider 将 API key 放在 query parameter 中,可以这样改:

params["api_key"] = SERP_API_KEY

重点是把 request layer 和 parsing layer 分开。

Step 3:Normalize Google Maps Results

不同 API 可能会把 Maps results 放在不同 key 下,例如:

local_results
maps_results
places_results

使用 flexible parser 可以避免切换 provider 时 code 直接坏掉。

from datetime import datetime, timezone


def clean_text(value):
    if not value:
        return ""
    return " ".join(str(value).split())


def get_maps_results(serp_json):
    return (
        serp_json.get("local_results")
        or serp_json.get("maps_results")
        or serp_json.get("places_results")
        or []
    )


def normalize_maps_results(serp_json, query, location):
    results = get_maps_results(serp_json)
    collected_at = datetime.now(timezone.utc).isoformat()

    rows = []

    for index, item in enumerate(results, start=1):
        gps = item.get("gps_coordinates") or {}

        rows.append({
            "query": query,
            "location": location,
            "position": item.get("position") or item.get("rank") or index,
            "business_name": clean_text(item.get("title") or item.get("name")),
            "rating": item.get("rating"),
            "reviews": item.get("reviews") or item.get("reviews_count"),
            "business_type": clean_text(item.get("type") or item.get("category")),
            "address": clean_text(item.get("address")),
            "phone": clean_text(item.get("phone")),
            "website": item.get("website") or item.get("link") or "",
            "place_id": item.get("place_id") or item.get("data_id") or item.get("data_cid") or "",
            "latitude": gps.get("latitude"),
            "longitude": gps.get("longitude"),
            "collected_at": collected_at
        })

    return rows

这样可以得到一个干净、稳定的格式,适合放入 spreadsheet、dashboard 或 database。

Step 4:导出结果到 CSV

接着可以把 normalized rows 写入 CSV file。

import csv


CSV_COLUMNS = [
    "query",
    "location",
    "position",
    "business_name",
    "rating",
    "reviews",
    "business_type",
    "address",
    "phone",
    "website",
    "place_id",
    "latitude",
    "longitude",
    "collected_at"
]


def export_to_csv(rows, filename="google_maps_results.csv"):
    if not rows:
        print("No results to export.")
        return

    with open(filename, mode="w", newline="", encoding="utf-8") as file:
        writer = csv.DictWriter(file, fieldnames=CSV_COLUMNS)
        writer.writeheader()
        writer.writerows(rows)

    print(f"Exported {len(rows)} rows to {filename}")

完整代码

import os
import csv
import requests

from datetime import datetime, timezone


SERP_API_KEY = os.getenv("SERP_API_KEY")
SERP_API_ENDPOINT = os.getenv("SERP_API_ENDPOINT")

CSV_COLUMNS = [
    "query",
    "location",
    "position",
    "business_name",
    "rating",
    "reviews",
    "business_type",
    "address",
    "phone",
    "website",
    "place_id",
    "latitude",
    "longitude",
    "collected_at"
]


def clean_text(value):
    if not value:
        return ""
    return " ".join(str(value).split())


def search_google_maps(query, location="United States"):
    params = {
        "engine": "google_maps",
        "query": query,
        "location": location,
        "output": "json"
    }

    headers = {
        "Authorization": f"Bearer {SERP_API_KEY}"
    }

    response = requests.get(
        SERP_API_ENDPOINT,
        params=params,
        headers=headers,
        timeout=30
    )

    response.raise_for_status()
    return response.json()


def get_maps_results(serp_json):
    return (
        serp_json.get("local_results")
        or serp_json.get("maps_results")
        or serp_json.get("places_results")
        or []
    )


def normalize_maps_results(serp_json, query, location):
    results = get_maps_results(serp_json)
    collected_at = datetime.now(timezone.utc).isoformat()

    rows = []

    for index, item in enumerate(results, start=1):
        gps = item.get("gps_coordinates") or {}

        rows.append({
            "query": query,
            "location": location,
            "position": item.get("position") or item.get("rank") or index,
            "business_name": clean_text(item.get("title") or item.get("name")),
            "rating": item.get("rating"),
            "reviews": item.get("reviews") or item.get("reviews_count"),
            "business_type": clean_text(item.get("type") or item.get("category")),
            "address": clean_text(item.get("address")),
            "phone": clean_text(item.get("phone")),
            "website": item.get("website") or item.get("link") or "",
            "place_id": item.get("place_id") or item.get("data_id") or item.get("data_cid") or "",
            "latitude": gps.get("latitude"),
            "longitude": gps.get("longitude"),
            "collected_at": collected_at
        })

    return rows


def export_to_csv(rows, filename="google_maps_results.csv"):
    if not rows:
        print("No results to export.")
        return

    with open(filename, mode="w", newline="", encoding="utf-8") as file:
        writer = csv.DictWriter(file, fieldnames=CSV_COLUMNS)
        writer.writeheader()
        writer.writerows(rows)

    print(f"Exported {len(rows)} rows to {filename}")


if __name__ == "__main__":
    query = "coffee shops in Seattle"
    location = "Seattle, Washington, United States"

    serp_json = search_google_maps(query, location)
    rows = normalize_maps_results(serp_json, query, location)
    export_to_csv(rows)

执行后,会生成一个文件:

google_maps_results.csv

获取更好 Google Maps Data 的建议

使用具体 local queries。
coffee shop in Seattle 会比 coffee 更清楚。

仔细控制 location。
City、region、country 和 coordinates 都可能改变结果。

保存 timestamps。
Local rankings 会随时间变化,因此每条 record 都应包含 collected_at

保留 place identifiers。
place_iddata_iddata_cid 可以帮助你在多次抓取中匹配同一个 business。

先 normalize 再存储。
不要让 dashboard 直接依赖某个 provider 的 raw response format。免费测试SERP API

FAQ

可以不用 SERP API 直接采集 Google Maps results 吗?

可以,但会更难。你可能需要 browser automation、proxy handling、CAPTCHA handling、scrolling logic 和 HTML parsing。SERP API 通常能更快提供 structured data。

可以从 Google Maps results 中获取哪些字段?

常见字段包括 business name、ranking position、rating、review count、address、phone number、website、business category、place ID 和 GPS coordinates。

Google Maps results 可以用于 local SEO 吗?

可以。Local SEO 团队常用 Maps data 监测 rankings、比较 competitors、追踪 ratings,并生成 client reports。

Google Maps results 应该保存为 CSV 还是 database?

小型测试和报告可以用 CSV。如果要对多个 keywords、cities 和 clients 做 recurring monitoring,database 会更合适。

立即开展您的数据业务

加入全球最强大的代理网络

免费试用