---
title: "Discussion on \"Extract every URL from a sitemap.xml in Python (sitemap index, .gz, robots.txt)\" | Hashnode"
description: "Discussion on \"Extract every URL from a sitemap.xml in Python (sitemap index, .gz, robots.txt)\". You need every URL a site advertises to crawlers: for a content audit, a change detector, or a broken-link sweep. Most sites put them in sitemap.xml, sometimes nested behind a sitemap index, sometimes"
url: "https://hashnode.com/posts/extract-every-url-from-a-sitemap-xml-in-python-sitemap-index-gz-robots-txt/6ad0bbad528de36d0eba40ab"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Extract every URL from a sitemap.xml in Python (sitemap index, .gz, robots.txt)](https://saadhazz.hashnode.dev/extract-every-url-from-a-sitemap-xml-in-python-sitemap-index-gz-robots-txt?utm_source=hashnode&utm_medium=feed)

You need every URL a site advertises to crawlers: for a content audit, a change detector, or a broken-link sweep. Most sites put them in sitemap.xml, sometimes nested behind a sitemap index, sometimes

Oct 15 · 6 min read · saadhazz.hashnode.dev  
Updated Oct 20  
Read the article

-   [#python](https://hashnode.com/tag/python)
-   [#seo](https://hashnode.com/tag/seo)
-   [#webscraping](https://hashnode.com/tag/webscraping)
-   [#tutorial](https://hashnode.com/tag/tutorial)

## Written by

[Saad | Data APIs](https://hashnode.com/@saadhazz)

@saadhazz · Saudi Arabia

  
[View profile](https://hashnode.com/@saadhazz)

Building data APIs on Apify

I build data APIs for developers: job listings from 13k company career sites, bulk WHOIS/RDAP domain checks, sitemaps, PDFs and SEC filings. Python, scraping without the pain, pay-per-result.

## Responses 2

Markdown supported · ⌘↵ to post

[OnlineProxy](https://hashnode.com/@onlineproxyio)

[Oct 16](https://hashnode.com/posts/extract-every-url-from-a-sitemap-xml-in-python-sitemap-index-gz-robots-txt/6ad0bbad528de36d0eba40ab/comment/6ad23d112eaea1c09727e771)

the gzip check goes by the URL ending and content type, so a sitemap.xml.gz?ts=123 with a generic type might slip through, and checking data.startswith(b"\\x1f\\x8b") would cover that. root.iter() can also pick up image loc entries, so findall with the unused NS map plus the current loop as a fallback could keep things clean.

for robots.txt, cutting anything after a hash and stripping the line first would handle trailing comments and indented Sitemap lines. might also be worth a size cap in fetch, since one big compressed sitemap gets fully decompressed into memory

[Saad | Data APIs](https://hashnode.com/@saadhazz)

[Oct 20](https://hashnode.com/posts/extract-every-url-from-a-sitemap-xml-in-python-sitemap-index-gz-robots-txt/6ad0bbad528de36d0eba40ab/comment/6ad23d112eaea1c09727e771#reply-6ad7513f472e599946f9cba8)

Thanks, all four are fair and I've folded them into the script. It now sniffs the first two bytes (`\x1f\x8b`) instead of trusting the suffix or Content-Type, so `sitemap.xml.gz?ts=123` served with a generic type works. It takes only the `<loc>` that is a direct child of each `<url>`/`<sitemap>`, so `<image:loc>` from the image extension no longer leaks in. robots.txt lines are stripped and cut at `#` before matching `Sitemap:`. And fetch caps both the download and the decompressed size at 50 MB, the protocol's per-file limit, so a small .gz can't balloon in memory. I've updated the snippet in the post.
