Google 地圖評論批次擷取教學
如果你經營的是餐廳、咖啡館或任何需要客戶回饋的地方,Google 地圖上的評論不只可以提升曝光度,更能直接影響顧客決策。這篇文章會帶你一步步從「怎麼抓取」到「整理資料」完成全流程,讓你不用手動複製貼上就能一次搞定大量評論。
前置條件
- 需要一個 Google Cloud Platform 帳號並啟用 My Business API。若還沒開通,可參照官方文件簡單設定。
- 一部裝有 Chrome 或 Edge 的電腦,因為後面會用到 Selenium 抓取網頁資料。
主要抓取方式
- 官方 API:最穩定、合法的做法,但需要申請授權。適合長期、大量需求。
- 網頁爬蟲:利用 Python 的 Selenium + BeautifulSoup,模擬人為點擊翻頁來取得資料。速度快且不需額外費用,但要注意遵守 Google 使用條款。
- 第三方工具:市面上有不少 SaaS 服務可一次抓取多個商家評論,操作簡單但通常會收費。
官方 My Business API 抓取範例
以下示範如何使用 Python 的 google-api-python-client 套件,先取得授權後一次下載所有評論。請先確定你已經在 Google Cloud Console 產生了 OAuth 2.0 憑證。
安裝套件:pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
import os, json
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
1. 建立授權流程
SCOPE = ['https://www.googleapis.com/auth/business.manage']
flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPE)
creds = flow.run_local_server(port=0)
2. 建立服務物件
service = build('mybusinessbusinessinformation', 'v1', credentials=creds)
3. 找到你的商家帳號(地方)
accounts = service.accounts().list().execute()
account_id = accounts['accounts'][0]['name'].split('/')[-1]
print(f'使用帳號:{account_id}')
4. 取得所有位置(店鋪)
locations = service.accounts().locations().list(parent=f'accounts/{account_id}').execute()
5. 抓取每個位置的評論
all_reviews = []
for loc in locations.get('locations', []):
location_name = loc['name']
reviews_resp = service.accounts().locations().reviews().list(parent=location_name).execute()
all_reviews.extend(reviews_resp.get('reviews', []))
6. 輸出成 JSON 檔案
with open('google_reviews.json', 'w', encoding='utf-8') as f:
json.dump(all_reviews, f, ensure_ascii=False, indent=2)
print(f'已抓取 {len(all_reviews)} 筆評論,存檔為 google_reviews.json')
網頁爬蟲範例(Selenium + BeautifulSoup)
如果你想要快速測試或抓取少量資料,以下示例會打開 Chrome、進入指定店鋪的 Google 地圖評論區塊,自動點擊「顯示更多」直到無法再載入。
安裝套件:pip install selenium beautifulsoup4 webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import time, json
URL = 'https://www.google.com/maps/place/Your+Store+Name/reviews'
options = webdriver.ChromeOptions()
options.add_argument('--headless') # 若想看到畫面可移除
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get(URL)
time.sleep(3) # 等頁面載入
點擊「顯示更多」直到無法再點
while True:
try:
more_btn = driver.find_element_by_xpath("//button[contains(@aria-label,'顯示更多')]")
if more_btn.is_displayed():
driver.execute_script('arguments[0].click();', more_btn)
time.sleep(2) # 等新評論載入
else:
break
except Exception:
break
取得頁面 HTML,使用 BeautifulSoup 分析
soup = BeautifulSoup(driver.page_source, 'html.parser')
driver.quit()
解析評論區塊(示例,實際 selector 會根據 Google 地圖版型變動)
review_divs = soup.select('div[role="article"]')
reviews = []
for div in review_divs:
rating = div.find('span', attrs={'aria-label':True})
text = div.find('span', class_='section-review-text')
if rating and text:
reviews.append({
'rating': rating['aria-label'],
'text': text.get_text(strip=True)
})
輸出 JSON
with open('scraped_reviews.json', 'w', encoding='utf-8') as f:
json.dump(reviews, f, ensure_ascii=False, indent=2)
print(f'抓取 {len(reviews)} 筆評論,存檔為 scraped_reviews.json')
整理與分析小技巧
- 使用 Excel 或 Google Sheets 的 QUERY 函式快速篩選 4 顆星以下的評論。
- 若想做情感分析,可將 JSON 匯入 Python pandas,再用
TextBlob或VADER進行情緒判斷。
