pytrends が初回リクエストから 429・404 になる原因と、2026年に取れる全選択肢
time.sleep(10) を入れ、スマホのテザリングに切り替えても初回から HTTP 429 が出る。何が本当に壊れたのか、2026年時点で現実的な4つの選択肢のメリット・限界、そして移行手順を整理します。

根本原因
2025年4月アーカイブ
比較した選択肢
4つの現実解
ローカル実行
$0 無制限
429 回避
--egress actor-public
要点まとめ // TL;DR
GeneralMills/pytrendsは 2025 年 4 月 17 日にアーカイブ済み。古いNIDCookie ハンドシェイクで初回から 429 になり、trending_searches()は旧 API 廃止で 404 になります。- 2026 年の現実的な4択は、
request.py応急パッチ、Python 派生のtrendspy、商用 SERP API / 審査制公式 Alpha、そして MIT のgoogle-trends-for-agent(gtfa)です。 gtfaはローカル IP やHTTPS_PROXYで$0無制限動作し、gtfa nowで 404 を復旧、AWS/CI での 429 も--egress actor-publicの1フラグで回避できます。
01 // 何が本当に壊れたのか
アクセス過多ではない——pytrends は最初のリクエストで止まっている
たいていは月曜の朝に起きます。何ヶ月も静かに動き続けていた Google Trends の Python スクリプトが、突然 TooManyRequestsError: Google returned a response with code 429 で落ちる。最初は「速すぎたか」と思い time.sleep(10) を入れ、キーワードを1つに減らし、スマホ回線に変えてみる。それでも1回目のリクエストから 429 が返ります。
パッチを探して GitHub の GeneralMills/pytrends を開くと、上部に黄色いバナーが出ています。リポジトリは 2025年4月17日に読み取り専用でアーカイブされました。セッション初期化(GetGoogleCookie())が古い NID Cookie と同意フローのまま止まっているため、Google に最初から弾かれているのです。
アーカイブ前の Issue を追うと、実は3つの別の故障が重なっています。1つ目が古いハンドシェイクによる初回 429、2つ目が旧エンドポイント廃止による trending_searches() の 404/400、そして最も厄介な3つ目が、エラーにならなくても USER_TYPE_SCRAPER 判定で related_queries() に空データやブラウザと異なるダミー値が混ざる問題です。
gtfa)はローカル IP や HTTPS_PROXY で無料動作し、必要時のみ1フラグで住宅プロキシへ切り替えられます。02 // 2026年の全選択肢をフラットに比較
現在エンジニアが実際に使っている4つのルートと、それぞれのトレードオフ
書き換える前に、2026年時点で現実的に選ばれている4つの選択肢を正直に並べてみます。万能薬はなく、単発ノートブックか、日次 Cron か、商用パイプラインかで最適解が変わります。
| 選択肢 | 向いている用途 | コスト | 正直なトレードオフ |
|---|---|---|---|
| pytrends/request.py を手動修正(get → post) | 今日中に終わらせたい応急処置 | $0 | venv や Docker の再ビルドごとに消える。404 の trending_searches() は直らず、0〜100 のまま。 |
| Python コミュニティ派生(trendspy 等) | pip install だけで軽量に Python 完結したい | $0 | 手軽な移行先だが、AWS/CI の IP では 429 になりやすく、実検索ボリュームは出ない。 |
| 公式 Trends API(Alpha)または SerpApi / DataForSEO | 予算と SLA が必要な商用・高並列チーム | 審査制 / 月$50〜$150+ | 2025年7月発表の公式 Alpha は未だ審査制。商用 API は安定だが個人用途には高額。 |
| google-trends-for-agent(MIT CLI + MCP) | 個人開発・SEO/データ分析・AIエージェントで実検索数と429回避が欲しい | ローカル $0 · Apify 月$5無料枠 | 非公式エンドポイントのため大規模スクレイピングや SLA 用途には不向き。Node.js 20+ 必須(Python は subprocess 経由)。 |
03 // ステップ1 · 接続確認と interest_over_time() の置換
site-packages 改造をやめて、疎通確認から順に置き換える
MIT ライセンスの meka-im/google-trends-for-agent(npm 公開)へ移行する場合、まず既存コードの pytrends メソッドを gtfa コマンドと 1:1 で対応付けます。
request.py を書き換える代わりに、gtfa doctor --json で2秒で疎通を確認し、既存 Python スクリプトからは subprocess 経由で gtfa snapshot を呼び出します。
gtfa コマンドへそのまま対応します。# Old StackOverflow workaround: editing venv/.../pytrends/request.py line 88
# Breaks on every container rebuild and still returns uncalibrated 0..100 curves
from pytrends.request import TrendReq
pytrends = TrendReq(hl="en-US", tz=360, retries=3, backoff_factor=0.5)
pytrends.build_payload(["seo tools"], timeframe="today 12-m", geo="US")
# TooManyRequestsError: The request failed: Google returned a response with code 429
df = pytrends.interest_over_time()import json, subprocess
# 1. Verify Google Trends reachability from your machine first:
# npx -y -p google-trends-for-agent gtfa doctor --json
# 2. Drop-in replacement inside existing Python scripts:
res = subprocess.run(
[
"npx", "-y", "-p", "google-trends-for-agent",
"gtfa", "snapshot", "seo tools",
"--geo", "US", "--time", "today 12-m", "--json",
],
capture_output=True, text=True, check=True,
)
payload = json.loads(res.stdout)
print(payload["traffic"]) # Calibrated daily searches + series + explicit statusnpm install -g google-trends-for-agent で一度グローバル導入しておくと、npx の起動待ちなしで gtfa を直接呼べます。
04 // ステップ2 · 404 で止まった trending_searches() を復旧
廃止された trending_searches() を gtfa now に置き換える
pytrends.trending_searches(pn='united_states') で日次トレンドを集めていた場合、Cookie をどれだけ直しても復旧しません。Google が旧 JSON エンドポイント自体を終了したためです。
gtfa now は現行の Trending Now フィードから最大 30 か国の急上昇ワードを1回で取得し、時間窓(--hours 4|24|48|168)と最小検索数(--min-volume)で絞って CSV 出力できます。
from pytrends.request import TrendReq
pytrends = TrendReq(hl="en-US", tz=360)
# Fails with ResponseError: The request failed: Google returned a response with code 404
daily = pytrends.trending_searches(pn="united_states")
print(daily.head(20))# Pull live 24h trending searches across US, GB, and JP into a CSV file
npx -y -p google-trends-for-agent gtfa now \
--geo US,GB,JP \
--hours 24 \
--min-volume 50000 \
--limit 20 \
--format csv > trending_today.csv05 // ステップ3 · クラウド CI の 429 対策と選び方
AWS / GitHub Actions でまだ 429 が出るとき——そして最終的な選び方
クライアントを替えても、AWS・GCP・GitHub Actions などの共有データセンター IP は Google に厳しく制限されます。手元では $0 無制限(HTTPS_PROXY 対応)ですが、クラウド上で 429 に当たったらプロキシ回転を自作する必要はありません。APIFY_TOKEN を設定して --egress actor-public を足すだけです。
この1フラグだけで、オープンソースの Apify Actor 経由の住宅プロキシへ切り替わり、待機コスト $0・Apify の月 $5 無料枠で約 3,000〜7,000 クエリを回せます。
pip installだけで手元の Python で手軽に 0〜100 グラフを出したい →trendspyなどの派生ライブラリ。- SLA と高並列が必須の商用パイプライン → SerpApi / DataForSEO(または公式 Alpha 申請)。
- ローカル
$0・クラウド 429 の1フラグ回避・実日次検索数・AIエージェント MCP 対応をまとめて欲しい → google-trends-for-agent。
import time
from pytrends.request import TrendReq
pytrends = TrendReq(
proxies=["http://user:pass@proxy1:8000", "http://user:pass@proxy2:8000"],
retries=5, backoff_factor=1.0,
)
for kw in ["ai video", "voice agent", "mcp server"]:
pytrends.build_payload([kw], timeframe="now 7-d", geo="US")
# Still dies mid-batch when a proxy IP gets challenged
df = pytrends.interest_over_time()
time.sleep(2)# Local machine ($0 unlimited) or your own HTTPS_PROXY:
gtfa compare "ai video" "voice agent" "mcp server" --geo US --calibrate --format csv
# In CI / AWS / GitHub Actions when datacenter IPs hit HTTP 429:
export APIFY_TOKEN="apify_api_..."
gtfa compare "ai video" "voice agent" "mcp server" \
--geo US --calibrate --egress actor-public --format csv次の一歩:0〜100 スコアを実際の1日検索ボリュームへ変える
429 を抜けた次に多い落とし穴が、相対スコア「100」の読み違いです。Google Trends から実際の1日検索ボリュームを取得する方法 や インタラクティブ製品ページ もあわせてご覧ください。
FAQ
よくある質問(FAQ)
- なぜ pytrends は1回目のリクエストから HTTP 429 を返すのですか?
- GeneralMills/pytrends は 2025 年 4 月 17 日に読み取り専用でアーカイブされ、GetGoogleCookie() が古い NID Cookie と同意フローのまま止まっているため、きれいな住宅回線 IP でも最初から 429 で拒否されます。
- pytrends/request.py をパッチすれば trending_searches() の 404 も直りますか?
- 直りません。request.py の修正は一時的な Cookie 対策に過ぎず、venv や Docker 再構築で消えます。trending_searches() の旧エンドポイントは Google 側で廃止済みのため、gtfa now への移行が必要です。
- google-trends-for-agent は既存の Python スクリプトから使えますか?
- はい。Node.js 20+ で npm install -g google-trends-for-agent を入れれば、Python から subprocess 経由で gtfa snapshot や gtfa compare --format csv を呼び、そのまま json.loads() や pandas.read_csv() に渡せます。