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:
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)
$ 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:
| Site | Unique URLs | Time | Notes |
|---|---|---|---|
| astro.build | 352 | 1.2 s | One index, one child sitemap, no lastmod values |
| docs.python.org | 7 | 0.6 s | Sitemap only lists the version roots |
| www.gov.uk | 878,509 | 11 to 36 s | Index 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:
- Unescaped ampersands. A URL like
?a=1&b=2written with a raw&makes the XML invalid, andElementTreethrowsParseErrorfor the whole file, losing every valid URL in it too. - Soft-404s. Many sites answer
/sitemap.xmlwith their normal HTML page and a 200 status. The script crashes trying to parse it as XML. - Odd formats. Plain-text sitemaps (one URL per line), RSS and Atom feeds, and namespace prefixes like
ns0:urlall return nothing. - No retries. One 429 or timeout on child sitemap 30 of 36 and you lose the run.
- Extensions ignored. Image, video, news and hreflang entries are skipped.
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:
{
"sites": ["gov.uk", "docs.python.org"],
"maxUrlsPerSite": 8,
"checkStatus": true
}
15 URLs with status checks in 1.8 seconds. A selection:
| url | lastmod | status | sitemapUrl |
|---|---|---|---|
| https://www.gov.uk/ | – | 200 | /sitemaps/sitemap_1.xml |
| https://www.gov.uk/government/publications/tracking-international-graduate-outcomes-2011 | 2026-01-07T18:01:04Z | 200 | /sitemaps/sitemap_1.xml |
| https://www.gov.uk/hmrc-internal-manuals/labour-provider-operational-guidance/lpog3250 | 2026-09-30T09:27:41Z | 200 | /sitemaps/sitemap_1.xml |
| https://www.gov.uk/government/publications/insurance-premium-tax-increase-of-standard-rate | 2026-09-30T09:07:30Z | 200 | /sitemaps/sitemap_1.xml |
| https://www.gov.uk/government/organisations/office-of-the-secretary-of-state-for-scotland/about | 2026-10-13T10:43:25Z | 200 | /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
- One site, once: the script.
- One giant site: the script with
iterparse, or the actor with a cost cap if you don't want to maintain code. For millions of URLs from one site, the script is far cheaper. - Hundreds of sites, or sites you don't control: the actor. Broken sitemaps are the norm across a long list, and per-site error reporting saves the most time there.
- Pages that aren't in any sitemap: neither. Use a crawler.
Things people build with sitemap URLs
- Competitor content monitoring. Run daily with a date filter and you get only the pages they published or changed:
{
"sites": ["competitor-one.com", "competitor-two.com"],
"includeUrlPatterns": ["/blog/"],
"modifiedAfter": "1 day",
"maxUrlsPerSite": 0
}
- SEO audits.
checkStatuslists sitemap URLs that return 301 or 404, which you shouldn't be submitting to Google. - Feeding a RAG pipeline.
includeUrlPatterns: ["/docs/"]gives you a clean list for a content crawler. - Sizing a prospect. URL count is a rough proxy for how big a site is. Combine it with a tech stack lookup.
- Keyword gaps. Competitor URL slugs next to autocomplete keywords show topics they cover and you don't.
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.