A domain rank checker must match hostnames at their boundary. A string containing your domain can belong to another site, while a valid subdomain may belong to the target. Keep a missing rank as missing and record the depth that was actually checked.
Match the hostname, not a substring
from urllib.parse import urlsplit
def domain_rank(rows, domain):
domain = domain.casefold().strip('.')
for row in rows:
host = (urlsplit(row['link']).hostname or '').casefold()
if host == domain or host.endswith('.' + domain):
return row.get('position') # Missing rank remains missing, not zero.
return None
rows = [{"link": "https://searchcans.com.evil.test", "position": 1},
{"link": "https://www.searchcans.com/guide", "position": 3}]
assert domain_rank(rows, "searchcans.com") == 3
assert domain_rank(rows, "missing.test") is None
This example treats the target domain and its subdomains as a match. If the application needs one exact host only, use an explicit exact-host policy. Do not silently switch those policies between runs.
Preserve the observation boundary
| Field | Reason |
|---|---|
| Query and engine | Identify the search observation |
| Market and requested depth | Define where and how far the check looked |
| Matched result URL | Make the hostname decision reviewable |
| Observed position or missing value | Avoid inventing a rank beyond checked depth |
| Collection status | Separate failed collection from absence |
Test the false positives first
The local cases cover a lookalike suffix, a valid subdomain and an absent domain. Add your organization’s actual hostname policy and redirect behavior to the acceptance set. A parser test does not establish that a keyword has traffic or that a observed rank change has a particular algorithmic cause.
Use the tracking workflow for scheduling and history storage. This page isolates the hostname decision so a tracker does not collect misleading rankings.
Reference documentation: docs.python.org — /3/library/urllib.parse.html; www.searchcans.com — /google-search-api.