Keyword research is core to SEO. In this guide we’ll use Python with the Google Keyword Planner API to pull keyword ideas at scale, then export them to CSV for analysis and planning.
google-ads.yaml
credentials file (Developer token, OAuth client, refresh token, login-customer-id if needed).pip install google-ads
Updates change over time; adjust versions/fields if Google updates the SDK.
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.v16.services.types import GenerateKeywordIdeasRequest
import csv
# Load credentials from google-ads.yaml in the same directory or HOME path
client = GoogleAdsClient.load_from_storage(path="google-ads.yaml")
# Replace with your Google Ads customer ID (no hyphens), language & location constants
CUSTOMER_ID = "0000000000"
LANGUAGE_ID = "1000" # English
LOCATION_IDS = ["2826"] # United Kingdom (example geo target)
def keyword_constant(resource_type, value):
svc = client.get_service(resource_type)
return getattr(svc, f"{resource_type[0].lower()+resource_type[1:-7]}_path")(value)
def generate_ideas(seed_keywords):
kp_service = client.get_service("KeywordPlanIdeaService")
request = GenerateKeywordIdeasRequest(
customer_id=CUSTOMER_ID,
language=client.get_service("LanguageConstantService").language_constant_path(LANGUAGE_ID),
geo_target_constants=[
client.get_service("GeoTargetConstantService").geo_target_constant_path(loc)
for loc in LOCATION_IDS
]
)
request.keyword_seed.keywords.extend(seed_keywords)
resp = kp_service.generate_keyword_ideas(request=request)
rows = []
for r in resp.results:
metrics = r.keyword_idea_metrics
rows.append([
r.text,
metrics.avg_monthly_searches,
metrics.competition.name if metrics.competition is not None else "UNSPECIFIED"
])
return rows
if __name__ == "__main__":
ideas = generate_ideas(["seo consulting", "technical seo audit", "local seo services"])
with open("keyword_ideas.csv", "w", newline="", encoding="utf-8") as f:
w = csv.writer(f)
w.writerow(["Keyword", "Avg Monthly Searches", "Competition"])
w.writerows(ideas)
print("Saved keyword_ideas.csv")