Skip to main content

Simple sitemap parser for Python

Project description

Sitemap Parser

Robot searching for sitemaps

This is a Python library designed to parse XML sitemaps and sitemap index files from a given URL. It supports both standard XML sitemaps (which contain URLs) and sitemap index files (which contain links to other sitemaps). This tool is useful for extracting data such as URLs and modification dates from website sitemaps.

Acknowledgments

This is a fork of Dave O'Connor's site-map-parser. I couldn't have done this without his original work.

Installation

uv add py-sitemap-parser

Usage

The library provides a SiteMapParser class that can be used to parse sitemaps and sitemap indexes. You can pass a URL or raw XML data to the parser to extract the URLs or links to other sitemaps.

Parsing a Sitemap from a URL

import logging
from typing import TYPE_CHECKING

from py_sitemap_parser import SiteMapParser

if TYPE_CHECKING:
    from py_sitemap_parser import SitemapIndex
    from py_sitemap_parser import UrlSet

logging.basicConfig(level=logging.INFO, format="%(message)s")
logger: logging.Logger = logging.getLogger(__name__)


# url = "https://ttvdrops.lovinator.space/sitemap.xml"  # Sitemap index
url = "https://ttvdrops.lovinator.space/sitemap-static.xml"  # Sitemap with URLs
parser = SiteMapParser(source=url)

if parser.has_sitemaps():
    sitemaps: SitemapIndex = parser.get_sitemaps()
    for sitemap in sitemaps:
        logger.info(sitemap)

elif parser.has_urls():
    urls: UrlSet = parser.get_urls()
    for url in urls:
        logger.info(url)

Parsing a Raw XML String

from py_sitemap_parser import SiteMapParser, UrlSet

xml_data = """
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://example.com/</loc>
        <lastmod>2023-09-27</lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    <url>
        <loc>https://example.com/about</loc>
        <lastmod>2023-09-27</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
</urlset>
"""
parser = SiteMapParser(source=xml_data, is_data_string=True)
urls: UrlSet = parser.get_urls()
for url in urls:
    print(url)

# Output:
# - https://example.com/
# - https://example.com/about

Exporting Sitemap Data to JSON

You can export the parsed sitemap data to a JSON file using the JSONExporter class.

import json
import logging

from py_sitemap_parser import JSONExporter
from py_sitemap_parser import SiteMapParser

logging.basicConfig(level=logging.INFO, format="%(message)s")
logger: logging.Logger = logging.getLogger(__name__)

# Sitemap with URLs to other sitemaps
parser = SiteMapParser(source="https://ttvdrops.lovinator.space/sitemap.xml")

if parser.has_sitemaps():
    json_data: str = JSONExporter(data=parser).export_sitemaps()
    json_data = json.loads(json_data)
    logger.info("Exported sitemaps: %s", json_data)

logger.info("----" * 10)

# Sitemap with "real" URLs
parser2 = SiteMapParser(
    source="https://ttvdrops.lovinator.space/sitemap-static.xml",
)

if parser2.has_urls():
    json_data: str = JSONExporter(data=parser2).export_urls()
    json_data = json.loads(json_data)
    logger.info("Exported URLs: %s", json_data)

Converting Sitemap XML to a Python dict

If you'd like to work with the parsed sitemap as a plain Python dictionary, you can use SiteMapParser.to_dict().

from py_sitemap_parser import SiteMapParser

xml = """
<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">
    <url>
        <loc>https://example.com/</loc>
    </url>
</urlset>
"""

parser = SiteMapParser(source=xml, is_data_string=True)
parsed = parser.to_dict()

# xmltodict represents repeated elements as lists
print(parsed["urlset"]["url"][0]["loc"])

You can also enable namespace processing for expanded namespace keys:

parsed = parser.to_dict(process_namespaces=True)

Disabling Logging

If you want to disable logging, you can adjust the logging level to logging.CRITICAL or higher. This will suppress all log messages below the CRITICAL level.

Here's an example of how to do this:

import logging

# Set the logging level to CRITICAL to disable logging
logging.getLogger("sitemap_parser").setLevel(logging.CRITICAL)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_sitemap_parser-2.0.1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_sitemap_parser-2.0.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file py_sitemap_parser-2.0.1.tar.gz.

File metadata

  • Download URL: py_sitemap_parser-2.0.1.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for py_sitemap_parser-2.0.1.tar.gz
Algorithm Hash digest
SHA256 0ac4a370a215e4210c63b2e2da2073da4aa0e3e33e7a098ff26fc71b08f89ff3
MD5 7e19aec6d5ce3967230c2bd1a890259d
BLAKE2b-256 bb8123bb944025afb1c99e043bab3026292f0d89a66a6c32df1398a772ff1764

See more details on using hashes here.

File details

Details for the file py_sitemap_parser-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: py_sitemap_parser-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for py_sitemap_parser-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9a98400a85cd888139fdf42065f2a8c4890b683c35f0ad34c3d3d805d61fdf7f
MD5 3db43876522d711ce071116840c54cd0
BLAKE2b-256 b05ad4610e394c8113c6f70c66cee0053b2b159660d4d6132c86996bf7258cdc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page