How to get all URLs of a website from its sitemap

Last updated · by Sharkinho Tools team

Recipe card

You get: A deduplicated list of page URLs with lastmod, from every sitemap the site publishes

Time
2 minutes
Difficulty
Easy
Tool
Sitemap URL Extractor

Ingredients

  • A domain
  • Python 3 with requests, or a free Apify account

Quick answer: Read the site's XML sitemaps: find them through the Sitemap: lines in robots.txt (or /sitemap.xml), follow every child sitemap in the index, and collect each <loc> with its <lastmod>. A 50-line Python script does this for most sites. It won't find pages the owner left out of the sitemap; for those you need a crawler.

This is one of the few recipes on the site where we'd tell most people to use the free script below and close the tab. Sitemaps are public files that owners publish for search engines, and parsing them is simple. Our actor earns its keep on the awkward cases: hundreds of sites at once, broken XML, status checks and scheduled change monitoring.

Get all URLs from a sitemap with Python

Needs only pip install requests. It reads robots.txt, follows nested indexes, and unzips gzip files by their magic bytes:

python
import gzip
import sys
import xml.etree.ElementTree as ET

import requests

HEADERS = {"User-Agent": "sitemap-reader/1.0 (+https://scrapehow.com)"}
NS = "{http://www.sitemaps.org/schemas/sitemap/0.9}"


def fetch(url):
    r = requests.get(url, headers=HEADERS, timeout=30)
    r.raise_for_status()
    body = r.content
    if body[:2] == b"\x1f\x8b":  # gzip magic bytes, whatever the headers say
        body = gzip.decompress(body)
    return body


def find_sitemaps(domain):
    robots = requests.get(f"https://{domain}/robots.txt", headers=HEADERS, timeout=30)
    found = [line.split(":", 1)[1].strip()
             for line in robots.text.splitlines()
             if line.lower().startswith("sitemap:")]
    return found or [f"https://{domain}/sitemap.xml"]


def read_sitemap(url, seen=None, depth=0):
    seen = seen if seen is not None else set()
    if url in seen or depth > 5:
        return
    seen.add(url)
    root = ET.fromstring(fetch(url))
    if root.tag == NS + "sitemapindex":
        for loc in root.iter(NS + "loc"):
            yield from read_sitemap(loc.text.strip(), seen, depth + 1)
    else:
        for entry in root.iter(NS + "url"):
            loc = entry.findtext(NS + "loc", "").strip()
            lastmod = entry.findtext(NS + "lastmod")
            yield loc, lastmod


if __name__ == "__main__":
    domain = sys.argv[1]
    urls = {}
    for sm in find_sitemaps(domain):
        for loc, lastmod in read_sitemap(sm):
            urls.setdefault(loc, lastmod)
    print(f"{len(urls)} unique URLs")
    for loc, lastmod in list(urls.items())[:5]:
        print(lastmod or "-", loc)
output
$ python sitemap_urls.py astro.build
352 unique URLs
- https://astro.build/
- https://astro.build/agencies/
- https://astro.build/agencies/bejamas/
- https://astro.build/agencies/bits-and-letters/
- https://astro.build/agencies/join/

What it did on 22 November 2026:

SiteUnique URLsTimeNotes
astro.build3521.2 sOne index, one child sitemap, no lastmod values
docs.python.org70.6 sSitemap only lists the version roots
www.gov.uk878,50911 to 36 sIndex of 36 sitemaps; peak memory 426 MB

Honestly, it did better than we expected on gov.uk. The weak spot is memory: ET.fromstring loads each file whole, and the urls dict keeps every URL. Fine on a laptop, not fine on a 256 MB cloud function. Switch to ET.iterparse and write rows to disk as you go if you're past a million URLs.

Where the script breaks

These are the cases we had to handle in the actor, and the script handles none of them:

If none of that applies to the sites you care about, you're done.

The no-code route: Sitemap URL Extractor

Sitemap URL Extractor takes domains or direct sitemap URLs. It reads robots.txt first, then tries the common paths, ignores HTML soft-404s, streams the XML so memory stays flat, and keeps going past malformed entries. We ran this:

json
{
  "sites": ["gov.uk", "docs.python.org"],
  "maxUrlsPerSite": 8,
  "checkStatus": true
}

15 URLs with status checks in 1.8 seconds. A selection:

urllastmodstatussitemapUrl
https://www.gov.uk/–200/sitemaps/sitemap_1.xml
https://www.gov.uk/government/publications/tracking-international-graduate-outcomes-20112026-01-07T18:01:04Z200/sitemaps/sitemap_1.xml
https://www.gov.uk/hmrc-internal-manuals/labour-provider-operational-guidance/lpog32502026-09-30T09:27:41Z200/sitemaps/sitemap_1.xml
https://www.gov.uk/government/publications/insurance-premium-tax-increase-of-standard-rate2026-09-30T09:07:30Z200/sitemaps/sitemap_1.xml
https://www.gov.uk/government/organisations/office-of-the-secretary-of-state-for-scotland/about2026-10-13T10:43:25Z200/sitemaps/sitemap_1.xml
https://docs.python.org/3.13/–200/sitemap.xml
https://docs.python.org/3/–200/sitemap.xml

Every record has the same fields: changefreq, priority, image, video and news data, hreflang alternates and redirect target are null when the sitemap doesn't have them. The input gov.uk was resolved to the www sitemaps through robots.txt.

Price: $0.00025 per URL ($0.25 per 1,000) and $0.0003 per status check. That run costs about $0.008 at list price. All 878,509 gov.uk URLs would cost about $220, which is the moment to remember the free script exists. Per-site summaries in the OUTPUT record explain zero-URL sites ("No sitemap found", "returned HTTP 403") and are free.

Which one to use

Things people build with sitemap URLs

json
{
  "sites": ["competitor-one.com", "competitor-two.com"],
  "includeUrlPatterns": ["/blog/"],
  "modifiedAfter": "1 day",
  "maxUrlsPerSite": 0
}

FAQ

How do I get all URLs of a website?

Read its XML sitemaps. Look for Sitemap: lines in /robots.txt, or try /sitemap.xml and /sitemap_index.xml, then follow every child sitemap in the index. That gives you every URL the owner wants search engines to know about. Pages missing from the sitemap need a crawler.

Where is a website's sitemap?

Usually listed in robots.txt as "Sitemap: https://…". If not, the common locations are /sitemap.xml, /sitemap_index.xml, /sitemap-index.xml and /wp-sitemap.xml on WordPress. Some sites gzip them as .xml.gz.

Is the sitemap a complete list of pages?

Not necessarily. docs.python.org lists 7 URLs in its sitemap, one per Python version, even though the docs have thousands of pages. The sitemap is what the owner chose to publish. For an SEO audit that is exactly what you want; for a full inventory you also need a crawl.

Can I get only new or updated pages?

Filter on <lastmod>. The actor takes modifiedAfter values like "1 day" or "2 weeks". This only works when the site fills lastmod honestly: astro.build, for example, has no lastmod at all.

How do I find broken links in a sitemap?

Run a HEAD request for each URL and keep anything that is not 200. The actor does this with checkStatus, reporting the first status code (such as 301 or 404) and the redirect target, at $0.0003 per URL checked.

Written by the Sharkinho Tools team. We build and maintain these scrapers, and every table on this page comes from a run we did ourselves. If something here is out of date, the actor's Issues tab on Apify reaches us directly.