Skip to content

How to Scrape Sitemaps to Uncover Hidden Scraping Treasures

Hey friend! Are you looking to expand your web scraping horizons? Well, I‘ve got just the trick for you – sitemaps. These handy little XML files index entire websites, exposing all the juicy pages ripe for scraping.

In this comprehensive guide, we‘ll dive deep into the world of sitemaps. I‘ll show you:

  • What sitemaps are and why they‘re a scraper‘s best friend
  • Where to find sitemaps on websites
  • How to parse and extract data from sitemaps in Python and JavaScript
  • Limitations and risks of relying solely on sitemaps
  • How to integrate sitemap scraping into a robust data discovery toolkit

So strap in, and let‘s uncover some hidden scraping treasures!

Sitemaps – A Scraper‘s Guide to Website Indexes

A sitemap is essentially a map of a website‘s content, laid out neatly in an XML file for crawlers to find.

Over 50% of the top 10,000 websites have sitemaps according to recent statistics. They‘ve become hugely popular with webmasters since sitemaps:

  • Help search engines index new or updated content
  • Provide metadata like page priority and change frequency
  • Give website overviews for screen readers and diagrams
  • Reduce crawler burden with a low bandwidth document

But most importantly for us scrapers, sitemaps are a fantastic way to discover new targets on a website. Rather than painstakingly spidering pages, we can grab a neatly organized list of URLs primed for scraping.

It‘s like finding the website‘s secret index served up just for us!

So if you‘re looking to expand to new sites or categories, sitemaps should be your first stop when scouting out a new target website.

Where to Find Sitemaps on Websites

Now that you know what sitemaps are, where do you find them?

First, look for the sitemap file hosted at the root of the website domain –

If it‘s not there, the next place to check is the trusty robots.txt file. This file provides guidelines for web crawlers on what they can and can‘t scrape. Helpfully for us, it also often contains the path to the sitemap:


Here‘s how to parse robots.txt programmatically to extract the sitemap URL:

# Python
from urllib.robotparser import RobotFileParser

rp = RobotFileParser(‘‘)

sitemap_url = rp.site_maps()[0] 
// JavaScript
const axios = require(‘axios‘)

const response = await axios.get(‘‘)

const sitemapUrl =
  .find(line => line.startsWith(‘Sitemap:‘))
  .split(‘Sitemap: ‘)[1]

This lookups the Sitemap: line and extracts the URL. If it‘s not present, we‘ll have to scout the site manually or use Google searches.

But fear not – over 75% of websites with sitemaps declare them in robots.txt. So there‘s a good chance it‘ll be waiting right there for you!

Crawlers Dig Sitemaps – You Should Too!

At this point, you may be wondering – if sitemaps help search engines index the site, why would I want to scrape them?

Excellent question! While sitemaps are designed for big, "rule-following" crawlers, as scrapers we can use them in some crafty ways:

Discover new targets

Sitemaps provide a tidy list of ALL pages on a site. This lets us uncover pages we didn‘t even know existed without having to crawl the whole site ourselves.

Understand site structure

Reviewing the sitemap gives us an overview of how content is organized and any major categories or sections.

Lower bandwidth costs

Rather than downloading 1000s of HTML pages, we can extract data directly from compact XML sitemaps.

Avoid blocks

Accessing "approved" crawler files may help avoid blocks compared to aggressive spidering of HTML content.

So sitemaps give us a leg up over standard crawlers in finding hidden data while staying under the radar. Now let‘s learn how to tap into these secret maps!

Parsing Sitemaps in Python and JavaScript

Alright, it‘s time to get our hands dirty. We‘ve got our sitemap URL, but how do we extract the data?

Sitemap XML follows a standard structure with <url> nodes:





Our goal is to extract all the <loc> URLs. We can parse XML in any language, but two popular choices are:

Python – Using lxml or BeautifulSoup

JavaScript – Using cheerio

Let‘s go through examples in both:

# Python - lxml

import requests
from lxml import etree

xml = requests.get(‘‘)
tree = etree.fromstring(xml.content)

namespaces = {‘ns‘: ‘‘}

locs = tree.xpath(‘//ns:url/ns:loc/text()‘, namespaces=namespaces)


Here we use lxml to parse the XML into a tree and then extract the text of all <loc> nodes with XPath.

// JavaScript - cheerio

const axios = require(‘axios‘)
const cheerio = require(‘cheerio‘)

const xml = await axios.get(‘‘) 

const $ = cheerio.load(, { xmlMode: true })

const locs = $(‘loc‘).map((i, el) => $(el).text()).get()


With cheerio we load the XML and use CSS selectors to extract the <loc> text values.

Both parsers give us a list of URLs to feed into our scrapers!

Comparing Python XML Parsers

Since Python has a few options for parsing XML, let‘s look at a quick comparison:

  • lxml – Very fast C implementation, great for large files.
  • ElementTree – Python standard library, simpler but slower than lxml.
  • BeautifulSoup – More focus on HTML parsing but can handle XML.

In most cases, I‘d recommend lxml since speed is important when parsing large sitemaps.

But BeautifulSoup may be easier if you‘re already familiar with it for HTML scraping. So consider your use case when choosing a parser!

Traversing Sitemap Indexes

As your newfound passion for sitemaps grows, you may encounter a special flavor – sitemap indexes!

These indexes act as a hub linking to separate sitemap files. Why use an index? Since the max URLs per sitemap is 50,000, indexes allow huge sites to split up content.

A sitemap index looks like:





To extract all the URLs from an index, we need to:

  1. Parse the index file
  2. Extract the nested sitemap links
  3. Fetch each linked sitemap
  4. Extract the <loc> URLs inside them

Here is how that traversal would work in Python:

import requests
from lxml import etree

# Parse index
index_xml = requests.get(‘‘)
index_tree = etree.fromstring(index_xml.content)

# Get sitemap locations
sitemaps = index_tree.xpath(‘//sitemap/loc/text()‘) 

locs = []

# Iterate through sitemaps
for sitemap in sitemaps:

  xml = requests.get(sitemap)
  tree = etree.fromstring(xml.content)

  # Extract loc URLs 


We parse the index, iterate through the linked sitemaps, and accumulate all the <loc> URLs.

So don‘t let indexes intimidate you – just follow the sitemap trail to scrape them all!

More Than Just URLs

So far we‘ve focused on the main <loc> URLs. But sitemaps can contain other handy tidbits as well:

  • <lastmod> – When the page was last modified
  • <changefreq> – How often the page changes
  • <priority> – Relative priority for crawlers
  • <images:image> – Associated images

For example, to grab last modified dates:

locs = []
lastmods = []

for url in tree.xpath(‘//url‘):

  loc = url.xpath(‘loc/text()‘)[0]
  lastmod = url.xpath(‘lastmod/text()‘)[0]



This extra metadata can help prioritize what and when to scrape. Images and videos are also useful for media scraping.

So don‘t ignore these bonus fields when parsing sitemaps!

Limitations of Sitemaps

While extremely useful, sitemaps do have some limitations to be aware of:

  • Not all sites have them – Sitemaps are optional, so many sites don‘t provide them.

  • Can be outdated – The URLs listed may not fully reflect the live site. Always verify the pages exist.

  • Blocked access – Some sites may block direct access to their sitemaps, requiring authentication.

  • Limited context – Sitemaps provide URLs but don‘t include info like titles, descriptions etc.

  • Constant change – New pages may be added/removed frequently, requiring re-scraping.

The main risk is relying solely on sitemaps for your discovery needs. Be sure to combine them with other techniques like:

  • Scraping site search engines and directories
  • Following links from page to page
  • Monitoring changes with tools like Visualping
  • Exploring the site manually for hidden sections

Use sitemaps as your trusty starting point, but don‘t stop your journey there!

Integrating Sitemaps into Your Data Discovery Toolkit

Alright my friend, we‘re nearing the end of our sitemap adventure. Let‘s bring it all together to integrate these techniques into a robust data discovery pipeline.

Here is a framework you can follow when exploring a new target site:

1. Consult robots.txt and find sitemap

First order of business is checking for a sitemap and grabbing the URL.

2. Extract sitemap URLs

Scrape the sitemap file to extract all the <loc> URLs. Handle indexes if needed.

3. Verify targets exist

Check that the pages still exist by sampling URLs. Weed out any dead ones.

4. Prioritize by metadata

Use last modified, change frequency etc. to prioritize what‘s most important.

5. Spider key pages

Manually browse key pages and scrape link URLs.

6. Monitor for changes

Use a tool like Visualping to watch for updates and new content.

7. Search site sections

Dig into site search and directories to find pages not in sitemap.

8. Iterate and expand

Rinse and repeat across categories, subdomains etc. to cover all data.

Following this game plan ensures you maximize value from sitemaps while still thoroughly mining the website.

The journey doesn‘t stop at the sitemap. But it‘s certainly a great place to start your expedition!

Go Forth and Scrape Those Sitemaps!

And with that, you‘ve got all the tools needed to harness the power of sitemaps and uncover hidden scraping treasures.

We covered:

  • What sitemaps are and why they‘re useful for scraping
  • Where to find sitemaps on websites
  • How to parse and extract data from sitemaps in Python and JavaScript
  • Limitations to be aware of like outdated data
  • How to integrate sitemap discovery into a robust pipeline

So now it‘s time for you to venture out, find some sitemaps, and scrape to your heart‘s content!

Wishing you happy data hunting my friend. May your sitemaps lead you to plentiful scraping opportunities!

Join the conversation

Your email address will not be published. Required fields are marked *