All notes

Check a whole sitemap with the URL Inspection API inside its daily limit

Google Search ConsolePythonSEOIndexing

Read the sitemap’s address from robots.txt, inspect a sample when the sitemap is large, retry Google’s 429 and 5xx answers, and count a URL that keeps failing instead of stopping. Those four changes took my weekly indexing check from failing on its first run to a 55-minute pass over a site with 1,817 URLs.

The check uses the service account from an earlier note. Every Sunday it asks the URL Inspection API about each URL in each site’s sitemap and saves how many Google has indexed. Google’s limit is 2,000 inspections a day and 600 a minute for each property.

Find the sitemap in robots.txt

The first run ended with a 404 on one site. The script asked for /sitemap-index.xml, which Astro’s sitemap integration writes, and that site serves /sitemap.xml. robots.txt already names the right file, so the script reads it there and guesses only when robots.txt has no Sitemap: line:

robots = fetch(f"https://{domain}/robots.txt") or ""
starts = re.findall(r"(?im)^\s*sitemap:\s*(\S+)", robots) or [
    f"https://{domain}/sitemap-index.xml", f"https://{domain}/sitemap.xml"]

A sitemap index is followed to the sitemaps it lists. Each <url> gives its <loc> and, when present, its <lastmod>.

Sample a large sitemap

That site publishes more than a hundred pages a day and passed 1,800 URLs in two weeks, so it would have crossed 2,000 within days. One inspection took about 6.6 seconds, which puts 2,000 of them at over three and a half hours. Past 500 URLs the script takes a sample:

def sample(entries, limit=500):
    if len(entries) <= limit:
        return [u for u, _ in entries]
    undated = [u for u, m in entries if not m]
    dated = [u for u, m in sorted((e for e in entries if e[1]), key=lambda e: e[1], reverse=True)]
    room = max(limit - len(undated), 0)
    newest, older = dated[:room // 2], dated[room // 2:]
    step = max(len(older) / max(room - len(newest), 1), 1)
    spread = [older[int(i * step)] for i in range(room - len(newest)) if int(i * step) < len(older)]
    return undated[:limit] + newest + spread

The pages without a date come first: the home page, the lists and the about pages. Half of what’s left goes to the newest pages, where indexing changes week to week. The other half is an even spread of the older ones, so they still count.

Retry, and keep going

A test run died after 37 minutes on a single 500 from Google. The call now retries 429, 500, 502, 503 and 504 three times, after 5, 20 and 45 seconds:

except urllib.error.HTTPError as e:
    if e.code in (429, 500, 502, 503, 504) and attempt < tries - 1:
        time.sleep(5 * (attempt + 1) ** 2)
        continue
    raise

A URL that still fails is counted under “inspection failed” and the run moves on. The run stops on a 401 or a 403, where no retry will help, and after more than ten failed URLs, where Google is failing throughout.

Failures now go to the systemd journal as well as to a phone notification. The first failure went only to the phone, and the journal had nothing to read afterwards.

The run

The site with 1,817 URLs took 55 minutes for its 500:

Google’s answer Pages
Submitted and indexed 214
URL is unknown to Google 198
Discovered - currently not indexed 87
Crawled - currently not indexed 1

The counts describe the 500 sampled pages, and the saved file carries both numbers, 500 inspected and 1,817 in the sitemap. The smaller sites fit under 500 and are inspected in full.