如何使用 SERP API 擷取 Google Maps 結果

了解如何使用 SERP API 和 Python 擷取 Google Maps 結果,提取商家名稱、評分、評論數、地址、電話、網站、place ID 和 GPS 座標,並匯出為 CSV。

talor ai
Last updated on
6 min read

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 會更合適。

Scale Your Data
Operations Today.

Join the world's most robust proxy network.

Start Free Trial