Python3 package for writing large XML sitemaps with no external dependencies
Project description
py-xml-sitemap-writer
Python3 package for writing large XML sitemaps with no external dependencies.
pip install xml-sitemap-writer
Usage
This package is meant to generate sitemaps with hundred of thousands of URLs in memory-efficient way by making use of iterators to populate sitemap with URLs.
from typing import Iterator
from xml_sitemap_writer import XMLSitemap
def get_products_for_sitemap() -> Iterator[str]:
"""
Replace the logic below with a query from your database.
"""
for idx in range(1, 1000001):
yield f"/product/{idx}.html" # URLs should be relative to what you provide as "root_url" below
with XMLSitemap(path='/your/web/root', root_url='https://your.site.io') as sitemap:
sitemap.add_section('products')
sitemap.add_urls(get_products_for_sitemap())
sitemap.xml
and sitemap-00N.xml.gz
files will be generated once this code runs:
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<!-- Powered by https://github.com/pigs-will-fly/py-xml-sitemap-writer -->
<!-- 100000 urls -->
<sitemap><loc>https://your.site.io/sitemap-products-001.xml.gz</loc></sitemap>
<sitemap><loc>https://your.site.io/sitemap-products-002.xml.gz</loc></sitemap>
...
</sitemapindex>
And gzipped sub-sitemaps with up to 15.000 URLs each:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>https://your.site.io/product/1.html</loc></url>
<url><loc>https://your.site.io/product/2.html</loc></url>
<url><loc>https://your.site.io/product/3.html</loc></url>
...
</urlset>
<!-- 15000 urls in the sitemap -->
For easier discovery of your sitemap add its URL to /robots.txt
file:
Sitemap: https://your.site.io/sitemap.xml
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
Built Distribution
File details
Details for the file xml_sitemap_writer-0.5.1.tar.gz
.
File metadata
- Download URL: xml_sitemap_writer-0.5.1.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c770b34b5fb9f2cf12af3278853971faf991948da43a1ec004a2533f4c08979 |
|
MD5 | 5ec6c57a6d1e1397ec7a2f04aee589d6 |
|
BLAKE2b-256 | d0c34213a30131a5b31cba18bccd7ddbae4ed47c09448a30ffa7043cf2792e38 |
File details
Details for the file xml_sitemap_writer-0.5.1-py3-none-any.whl
.
File metadata
- Download URL: xml_sitemap_writer-0.5.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79e3b693bb0a4678a045c9571c6acf8d3feb672f51081a3bd7247caf5bba1195 |
|
MD5 | 47b96fa87285e5b8246a486954f71876 |
|
BLAKE2b-256 | 016cbd3b31d22deeec8d7200790ea7984c9f9a436f8843e6f6b4196cae55500f |