Sitemap Parser
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)
Metadata
Release files for py-sitemap-parser 2.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| py_sitemap_parser-2.0.1.tar.gz | 8.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| py_sitemap_parser-2.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.7 kB
Release files / py_sitemap_parser-2.0.1.tar.gz
| Download URL | py_sitemap_parser-2.0.1.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0ac4a370a215e4210c63b2e2da2073da4aa0e3e33e7a098ff26fc71b08f89ff3
|
|
BLAKE2b-256 checksum How to use checksums |
bb8123bb944025afb1c99e043bab3026292f0d89a66a6c32df1398a772ff1764
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / py_sitemap_parser-2.0.1-py3-none-any.whl
| Download URL | py_sitemap_parser-2.0.1-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9a98400a85cd888139fdf42065f2a8c4890b683c35f0ad34c3d3d805d61fdf7f
|
|
BLAKE2b-256 checksum How to use checksums |
b05ad4610e394c8113c6f70c66cee0053b2b159660d4d6132c86996bf7258cdc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|