Search Console API ile tüm sorguları CSV'ye indirmek

Search Console arayüzü en fazla 1.000 satır gösteriyor. Orta büyüklükte bir sitede bile bu, uzun kuyruğun neredeyse tamamını görmemek demek. API ise istek başına 25.000 satıra kadar veriyor ve sayfalama yapabiliyorsun.

Hazırlık

  1. Google Cloud'da bir proje aç, Search Console API'yi etkinleştir.
  2. Bir servis hesabı oluştur, JSON anahtarını indir.
  3. Servis hesabının e-posta adresini Search Console'da mülke kullanıcı olarak ekle.
pip install google-api-python-client google-auth

Betik

gsc_export.py
import csv
from google.oauth2 import service_account
from googleapiclient.discovery import build

SITE = "sc-domain:ornek.com"
creds = service_account.Credentials.from_service_account_file(
    "anahtar.json", scopes=["https://www.googleapis.com/auth/webmasters.readonly"])
gsc = build("searchconsole", "v1", credentials=creds)

rows, start = [], 0
while True:
    resp = gsc.searchanalytics().query(siteUrl=SITE, body={
        "startDate": "2024-07-01",
        "endDate": "2024-09-30",
        "dimensions": ["query", "page"],
        "rowLimit": 25000,
        "startRow": start,
    }).execute()
    batch = resp.get("rows", [])
    rows += batch
    if len(batch) < 25000:
        break
    start += 25000

with open("gsc.csv", "w", newline="", encoding="utf-8") as f:
    w = csv.writer(f)
    w.writerow(["query", "page", "clicks", "impressions", "ctr", "position"])
    for r in rows:
        w.writerow([*r["keys"], r["clicks"], r["impressions"], round(r["ctr"], 4), round(r["position"], 1)])

print(f"{len(rows)} satır yazıldı")

Bir not

API'nin verdiği toplamlar arayüzdekilerle birebir tutmayabilir. Gizlilik nedeniyle çok nadir sorgular filtreleniyor. Bu normal; eksik olan veri değil, anonimleştirilmiş kuyruk.

Comments / questions

There's no comment section here. If you have a question or want to add something, message me on Telegram or send an email to [email protected]. Thanks!

Related pages