Local SEO rankings are not the same everywhere.
Your website may rank in position 2 in Austin, position 8 in Dallas, and not appear at all in Chicago.
That is why checking one generic Google result is not enough for local SEO.
If you are working on local SEO, agency reporting, competitor monitoring, or location-based search analysis, you need to track rankings by city.
A simple workflow looks like this:
Keyword + city → SERP API → local search results → ranking check → CSV report
In this tutorial, we’ll build a basic Python script that tracks local SEO rankings by city using a SERP API.
We will:
- Define target keywords
- Define target cities
- Send city-specific searches to a SERP API
- Extract organic results
- Check where a target domain appears
- Save the ranking data to CSV
This is not a full SEO platform, but it gives you the core logic behind many local rank tracking tools.
Why city-level rankings matter
Google search results are location-sensitive.
A query like:
best digital marketing agency
may return different results in:
New York
Austin
London
Singapore
Sydney
This matters even more for local intent keywords, such as:
dentist near me
plumber in Chicago
coffee shop in Austin
real estate agent in Miami
For these searches, the result page can include:
- organic results
- local packs
- Google Maps results
- ads
- business directories
- review sites
- service pages
- location-specific landing pages
If you only check one location, you may miss what users actually see in other cities.
For local SEO, ranking data without location context is incomplete.
Why use a SERP API?
You could try to check Google rankings manually.
But that does not scale.
You could also try to scrape Google directly, but that brings a lot of maintenance work:
- changing page layouts
- CAPTCHA
- blocked requests
- proxy handling
- inconsistent HTML
- location mismatch
- parser updates
- retry logic
A SERP API gives you structured search results in JSON.
Instead of parsing raw HTML, you get data like this:
{
"query": "plumber in Austin",
"location": "Austin, Texas, United States",
"organic_results": [
{
"position": 1,
"title": "Austin Plumbing Services",
"link": "https://example.com",
"snippet": "Local plumbing services in Austin..."
}
]
}
That is much easier to use in Python.
What we are building
Let’s say we want to track whether this target domain appears in city-level Google results:
example.com
We want to test keywords such as:
plumber
emergency plumber
water heater repair
Across cities such as:
Austin, Texas, United States
Dallas, Texas, United States
Houston, Texas, United States
For each keyword and city, we want to know:
- keyword
- city
- target domain
- whether the domain was found
- ranking position
- result title
- result URL
- snippet
Example CSV output:
keyword,city,target_domain,found,position,title,url
plumber,Austin,example.com,yes,3,Example Plumbing,https://example.com/austin
plumber,Dallas,example.com,no,,,
emergency plumber,Houston,example.com,yes,7,Emergency Plumbing,https://example.com/houston
Install dependencies
We only need two packages:
pip install requests python-dotenv
requests is used to call the SERP API.
python-dotenv is used to load API keys from a .env file.
Create a .env file
Create a file named .env:
SERP_API_KEY=your_api_key_here
SERP_API_URL=https://your-serp-api-endpoint.example.com/search
Do not hardcode API keys in your script.
In this tutorial, we will use a generic SERP API request format. Providers may use different parameter names, so adjust the request based on the API you use.
Create keyword and city lists
Create a file named keywords.txt:
plumber
emergency plumber
water heater repair
Create another file named cities.txt:
Austin, Texas, United States
Dallas, Texas, United States
Houston, Texas, United States
Each line is one keyword or one target city.
Basic SERP API request
Create a file named local_rank_tracker.py.
import os
import requests
from dotenv import load_dotenv
load_dotenv()
SERP_API_KEY = os.getenv("SERP_API_KEY")
SERP_API_URL = os.getenv("SERP_API_URL")
def fetch_google_results(query, location, language="en"):
if not SERP_API_KEY:
raise ValueError("Missing SERP_API_KEY environment variable")
if not SERP_API_URL:
raise ValueError("Missing SERP_API_URL environment variable")
params = {
"api_key": SERP_API_KEY,
"engine": "google",
"q": query,
"location": location,
"language": language,
"output": "json",
}
response = requests.get(SERP_API_URL, params=params, timeout=30)
response.raise_for_status()
return response.json()
The exact parameters may vary depending on your provider.
Some APIs may use:
q
query
gl
hl
country
city
location
locale
device
engine
The idea is the same: send a keyword and location, then get structured search results back.
Extract organic results
Most SERP APIs return organic results under a key such as:
organic_results
organic
results
Let’s make the parser flexible.
def get_organic_results(data):
possible_keys = [
"organic_results",
"organic",
"results",
]
for key in possible_keys:
value = data.get(key)
if isinstance(value, list):
return value
return []
Now normalize each organic result.
def normalize_result(item):
return {
"position": item.get("position") or item.get("rank"),
"title": item.get("title") or "",
"url": item.get("link") or item.get("url") or "",
"snippet": item.get("snippet") or item.get("description") or "",
}
This gives us a consistent format even if different APIs use slightly different field names.
Extract the domain from a URL
To check rankings, we need to compare the target domain with each result URL.
Use urllib.parse:
from urllib.parse import urlparse
def extract_domain(url):
if not url:
return ""
parsed = urlparse(url)
domain = parsed.netloc.lower()
if domain.startswith("www."):
domain = domain[4:]
return domain
This turns:
https://www.example.com/austin/plumbing
into:
example.com
Find the target domain ranking
Now we can check whether the target domain appears in the results.
def find_domain_ranking(results, target_domain):
target_domain = target_domain.lower().replace("www.", "")
for result in results:
result_domain = extract_domain(result.get("url", ""))
if result_domain == target_domain or result_domain.endswith("." + target_domain):
return {
"found": "yes",
"position": result.get("position"),
"title": result.get("title"),
"url": result.get("url"),
"snippet": result.get("snippet"),
}
return {
"found": "no",
"position": "",
"title": "",
"url": "",
"snippet": "",
}
This supports exact domain matches and subdomains.
For example, it can match:
example.com
www.example.com
blog.example.com
Load keywords and cities
Add this helper function:
def load_lines(filename):
with open(filename, "r", encoding="utf-8") as file:
return [
line.strip()
for line in file
if line.strip()
]
Now we can load:
keywords = load_lines("keywords.txt")
cities = load_lines("cities.txt")
Save results to CSV
Add a CSV helper:
import csv
def save_to_csv(rows, filename="local_rankings.csv"):
fieldnames = [
"keyword",
"city",
"target_domain",
"found",
"position",
"title",
"url",
"snippet",
]
with open(filename, mode="w", newline="", encoding="utf-8") as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
writer.writerow(row)
Full script
Here is the complete version:
import os
import csv
import requests
from urllib.parse import urlparse
from dotenv import load_dotenv
load_dotenv()
SERP_API_KEY = os.getenv("SERP_API_KEY")
SERP_API_URL = os.getenv("SERP_API_URL")
def fetch_google_results(query, location, language="en"):
if not SERP_API_KEY:
raise ValueError("Missing SERP_API_KEY environment variable")
if not SERP_API_URL:
raise ValueError("Missing SERP_API_URL environment variable")
params = {
"api_key": SERP_API_KEY,
"engine": "google",
"q": query,
"location": location,
"language": language,
"output": "json",
}
response = requests.get(SERP_API_URL, params=params, timeout=30)
response.raise_for_status()
return response.json()
def get_organic_results(data):
possible_keys = [
"organic_results",
"organic",
"results",
]
for key in possible_keys:
value = data.get(key)
if isinstance(value, list):
return value
return []
def normalize_result(item):
return {
"position": item.get("position") or item.get("rank"),
"title": item.get("title") or "",
"url": item.get("link") or item.get("url") or "",
"snippet": item.get("snippet") or item.get("description") or "",
}
def extract_domain(url):
if not url:
return ""
parsed = urlparse(url)
domain = parsed.netloc.lower()
if domain.startswith("www."):
domain = domain[4:]
return domain
def find_domain_ranking(results, target_domain):
target_domain = target_domain.lower().replace("www.", "")
for result in results:
result_domain = extract_domain(result.get("url", ""))
if result_domain == target_domain or result_domain.endswith("." + target_domain):
return {
"found": "yes",
"position": result.get("position"),
"title": result.get("title"),
"url": result.get("url"),
"snippet": result.get("snippet"),
}
return {
"found": "no",
"position": "",
"title": "",
"url": "",
"snippet": "",
}
def load_lines(filename):
with open(filename, "r", encoding="utf-8") as file:
return [
line.strip()
for line in file
if line.strip()
]
def save_to_csv(rows, filename="local_rankings.csv"):
fieldnames = [
"keyword",
"city",
"target_domain",
"found",
"position",
"title",
"url",
"snippet",
]
with open(filename, mode="w", newline="", encoding="utf-8") as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
writer.writerow(row)
def track_local_rankings(target_domain, keywords, cities, language="en"):
rows = []
for city in cities:
for keyword in keywords:
query = f"{keyword} in {city.split(',')[0]}"
print(f"Checking: {query} | Location: {city}")
try:
data = fetch_google_results(
query=query,
location=city,
language=language,
)
organic_items = get_organic_results(data)
normalized_results = [
normalize_result(item)
for item in organic_items
]
ranking = find_domain_ranking(
results=normalized_results,
target_domain=target_domain,
)
rows.append({
"keyword": keyword,
"city": city,
"target_domain": target_domain,
"found": ranking["found"],
"position": ranking["position"],
"title": ranking["title"],
"url": ranking["url"],
"snippet": ranking["snippet"],
})
except requests.RequestException as error:
print(f"Request failed: {query} | {city}")
print(error)
rows.append({
"keyword": keyword,
"city": city,
"target_domain": target_domain,
"found": "error",
"position": "",
"title": "",
"url": "",
"snippet": "",
})
return rows
if __name__ == "__main__":
target_domain = "example.com"
keywords = load_lines("keywords.txt")
cities = load_lines("cities.txt")
rankings = track_local_rankings(
target_domain=target_domain,
keywords=keywords,
cities=cities,
language="en",
)
save_to_csv(rankings, "local_rankings.csv")
print("Saved local ranking results to local_rankings.csv")
Run it:
python local_rank_tracker.py
You should get a CSV file:
local_rankings.csv
Example CSV output
Your output may look like this:
keyword,city,target_domain,found,position,title,url,snippet
plumber,Austin Texas United States,example.com,yes,3,Example Plumbing Austin,https://example.com/austin,Local plumbing services in Austin
plumber,Dallas Texas United States,example.com,no,,,,
emergency plumber,Houston Texas United States,example.com,yes,8,Emergency Plumbing Houston,https://example.com/houston,24/7 plumbing service
This gives you a basic city-level ranking report.
Track competitors by city
You can also track competitor domains.
For example:
target_domains = [
"example.com",
"competitor-one.com",
"competitor-two.com",
]
Then loop through them:
all_rows = []
for domain in target_domains:
rankings = track_local_rankings(
target_domain=domain,
keywords=keywords,
cities=cities,
language="en",
)
all_rows.extend(rankings)
save_to_csv(all_rows, "competitor_local_rankings.csv")
Now you can compare which domains appear most often across cities.
This can help answer questions like:
- Which competitor ranks in most cities?
- Which cities are weak for our domain?
- Which keywords need local landing pages?
- Which markets have the strongest competition?
Track city-level visibility score
A simple ranking position is useful, but sometimes you want a summary metric.
For example:
position 1 = 10 points
position 2 = 9 points
position 3 = 8 points
...
position 10 = 1 point
not found = 0 points
Add a scoring function:
def calculate_visibility_score(position):
if not position:
return 0
try:
position = int(position)
except ValueError:
return 0
if 1 <= position <= 10:
return 11 - position
return 0
Add it when creating the row:
score = calculate_visibility_score(ranking["position"])
Then include:
"visibility_score": score
This gives you a simple way to compare local visibility across cities.
Save city visibility summary
If you want to summarize visibility by city, you can calculate totals after collecting rows.
from collections import defaultdict
def summarize_by_city(rows):
summary = defaultdict(int)
for row in rows:
city = row["city"]
position = row.get("position")
summary[city] += calculate_visibility_score(position)
return [
{
"city": city,
"visibility_score": score,
}
for city, score in summary.items()
]
Then save it:
city_summary = summarize_by_city(rankings)
save_to_csv(city_summary, "city_visibility_summary.csv")
If you use a different CSV structure for summaries, create a separate save function:
def save_city_summary(rows, filename="city_visibility_summary.csv"):
fieldnames = [
"city",
"visibility_score",
]
with open(filename, mode="w", newline="", encoding="utf-8") as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
writer.writerow(row)
This is a simple but useful local SEO metric.
Using local ranking data with an LLM
Once you have local ranking data, you can feed it into an LLM to generate summaries.
Example prompt:
You are a local SEO analyst.
Here is city-level ranking data for our domain and competitors.
Summarize:
- where our domain ranks well
- which cities are weak
- which competitors appear most often
- which keywords need more local content
- what actions we should consider next
This can be useful for:
- local SEO reports
- agency client updates
- competitor research
- market expansion planning
- automated weekly summaries
The key is to give the LLM structured ranking data, not raw search pages.
What to check before choosing a SERP API
Before choosing a SERP API provider, test it with your real local queries.
Do not only test one easy keyword.
Try:
plumber in Austin
dentist near me
coffee shop in Seattle
real estate agent in Miami
best hotel near Times Square
Then check:
- Does the API return clean JSON?
- Does location targeting work correctly?
- Are titles, URLs, snippets, and positions available?
- Are local packs or maps results included if needed?
- Can you request HTML for debugging?
- Are failed requests billed?
- How much cleanup does your code need?
- Does the output fit your reporting workflow?
Providers such as SerpApi, Serper, SearchAPI, Bright Data, DataForSEO, and Talordata can all be tested for local ranking workflows.
The best provider is not always the one with the longest feature list.
It is the one that gives you usable local ranking data with the least extra work.
Common improvements
This script is only a starting point.
You could improve it by adding:
- scheduled daily or weekly runs
- database storage
- Google Maps local pack parsing
- mobile vs desktop tracking
- country and language controls
- competitor visibility scores
- Slack or email alerts
- ranking history charts
- city-level dashboards
- AI-generated SEO reports
For example, a weekly report could say:
example.com ranks in the top 3 for "plumber" in Austin and Houston, but is missing from the top 10 in Dallas.
That is much more useful than a generic ranking check.
Final thoughts
Local SEO ranking is location-specific.
A single Google result does not tell you how your domain performs across cities.
With Python and a SERP API, you can build a simple workflow to track rankings by keyword, city, and domain.
The core process is:
Keyword + city → SERP API → structured results → domain match → CSV report
From there, you can add competitor tracking, visibility scoring, city summaries, dashboards, and AI-generated reports.
If you want to test this workflow, Talordata is one SERP API worth comparing. Its official pages describe geo-targeted SERP data, JSON / HTML output, Maps search type support, and 1,000 free API responses after signup.
Top comments (0)