【2026年版】PythonでGoogle検索結果をCSV取得する方法|スクレイピング限界とSerpAPIによる安定解
Python × Google検索取得 × スクレイピングの現実
Google検索結果(SERP)をPythonで取得する処理は、かつてはスクレイピングで実現可能でした。
しかし現在は、
-
スクレイピング対策の強化
-
APIの制限・終了
-
フロント構造の変化
により、従来手法はほぼ破綻しています。
本記事では、
PythonでGoogle検索結果を安定取得する方法(非スクレイピング)
を、実装コード付きで解説します。
PythonでGoogle検索をスクレイピング取得できなくなった理由
■ 無限スクロール化
ページ番号の概念が消滅し、従来のページング取得が不可能に。
■ reCAPTCHAの強化
-
Selenium / Playwright → 高確率でブロック
-
ヘッドレス対策もほぼ無効
■ DOM構造の頻繁な変更
スクレイピングコードがすぐ壊れる。
■ Google公式APIの実質終了
-
Custom Search JSON API が新規利用不可
-
旧来ユーザーも2027年1月に終了予定
結論
PythonでGoogle検索結果を取得するには、スクレイピングは非現実的
解決策:SerpAPI(スクレイピング不要)
SerpAPIはGoogle検索結果をJSON形式で取得できる外部APIです。利用するにはSerpAPIのサイトで独自のAPIキーの取得が必要です。まずはそれを実施してください。
特徴
-
スクレイピング不要
-
reCAPTCHA回避済み
-
DOM変更の影響なし
-
ページング取得可能
-
ニュース・画像・ローカル検索対応
PythonでGoogle検索結果を取得(実装概要)
インストール
pip install google-search-results
実行例
python script.py "岡山 ダンス イベント"
実装で実現した機能
-
最大300件取得(30ページ)
-
重複URL除去
-
ドメイン抽出
-
順位(ページ_順位)
-
ニュース日付取得
-
CSV(Shift-JIS)出力
-
クエリ記録
-
ユニークファイル名
コード全文(Python / Google検索取得 / スクレイピング代替)
以下内容をscript.pyなどのファイル名で保存し、コマンドラインから以下を実行
python script.py "検索内容"
from serpapi import GoogleSearch import csv import sys from datetime import datetime from urllib.parse import urlparse API_KEY = "私のSerpAPIのAPIキー" # コマンドライン引数から query を取得 if len(sys.argv) < 2: print("検索キーワードを指定してください。例: python google_xxx.py \"岡山 ダンス イベント\"") sys.exit() query = sys.argv[1] rows = [] seen_urls = set() # 重複URL除外用 max_pages = 30 # 10件 × 30ページ = 最大300件 for page in range(max_pages): start = page * 10 page_rank = 1 # ページ内順位 params = { "engine": "google", "q": query, "start": start, "num": 10, "api_key": API_KEY } search = GoogleSearch(params) results = search.get_dict() candidate_fields = [ "organic_results", "local_results", "inline_results", "top_stories", "news_results" ] for field in candidate_fields: if field in results and results[field]: for item in results[field]: url = item.get("link") if not url: continue # 重複URL除外 if url in seen_urls: continue seen_urls.add(url) # ドメイン抽出 domain = urlparse(url).netloc # rank を「ページ番号-順位」形式にする rank = f"{page+1}_{page_rank}" page_rank += 1 # update日付(ニュース系) date = item.get("date") # 取得できる情報を最大限入れる rows.append([ rank, item.get("title"), url, domain, item.get("snippet") or item.get("description"), date, item.get("displayed_link"), item.get("source"), item.get("favicon"), item.get("thumbnail"), ]) # ▼ ユニークなファイル名: result-yyyymmdd-hhmm.csv timestamp = datetime.now().strftime("%Y%m%d-%H%M") filename = f"result-{timestamp}.csv" # ▼ CSV 出力(Shift-JIS) with open(filename, "w", newline="", encoding="cp932", errors="ignore") as f: writer = csv.writer(f) # 1行目に query writer.writerow([f"query: {query}"]) # ヘッダー writer.writerow([ "rank", "title", "url", "domain", "snippet", "date", "displayed_link", "source", "favicon", "thumbnail" ]) writer.writerows(rows) print(f"CSV 出力が完了しました: {filename}")
料金・制限(SerpAPI)
※2026年時点の概要
-
無料枠あり(100検索/月程度)
-
従量課金制
-
1リクエスト=1検索
注意点
-
大量取得するとコスト増
-
APIキー管理必須
Seleniumとの比較(重要)
| 手法 | 安定性 | 速度 | 保守 |
|---|---|---|---|
| スクレイピング | ❌ | 遅い | 地獄 |
| SerpAPI | ◎ | 高速 | 楽 |
実測イメージ
-
Selenium:約10〜15秒/ページ(失敗あり)
-
SerpAPI:約0.5秒/ページ(安定)
まとめ
PythonでGoogle検索結果を取得する方法は、
-
スクレイピング → ほぼ終了
-
公式API → 終了予定
という状況になっています。
その中で、
SerpAPIが実質的な標準解
です。
最終結論
PythonでGoogle検索結果を取得するなら、
スクレイピングではなくAPIを使うべき時代になった。


