sitemap

domonic can help create a sitemap or sitemap index for your website.

A sitemap contains URLs for your website. A sitemap index contains a list of sitemap files.

Below are examples of creating sitemaps with Python and domonic.

Creating a sitemap index

A sitemap index contains a list of sitemaps. A minimal one might look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
        <loc>http://www.example.com/sitemap1.xml.gz</loc>
        <lastmod>2004-10-01T18:23:17+00:00</lastmod>
</sitemap>
</sitemapindex>

With domonic, we can create one in a few different ways depending on our needs.

from domonic.html import render
from domonic.xml.sitemap import lastmod, loc, sitemap, sitemapindex

doc = sitemapindex(
        sitemap(
                loc("https://xyz.net/sitemap1.xml"),
                lastmod("2021-07-08T13:12:16+00:00"),
)
)

render(f"{doc}", "sitemap.xml")

Creating a sitemap

A sitemap contains URLs for your website and is limited to 50,000 URLs.

A minimal one might look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
        <loc>https://xyz.net/</loc>
        <lastmod>2004-10-01T18:23:17+00:00</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
</url>
</urlset>

With domonic, we can create one in a few different ways depending on our needs.

from domonic.html import render
from domonic.xml.sitemap import changefreq, lastmod, loc, priority, url, urlset

doc = urlset(
        url(
                loc("https://xyz.net"),
                lastmod("2021-07-08T13:12:16+00:00"),
                changefreq("weekly"),
                priority(0.5),
        )
)

# Use an f-string to prettify the document.
render(f"{doc}", "sitemap1.xml")

utils

domonic also has helpers for quickly creating sitemaps with default values.

from domonic.xml.sitemap import sitemap_from_urls

mypages = []
sm = sitemap_from_urls(mypages)
print(sm)
# <urlset></urlset>

You will often want a little more control, so use any DOM manipulation methods you like.

Here’s some more examples.

Creating a sitemap from scratch

from domonic.xml.sitemap import urlset, url, loc, lastmod

sm = urlset()
sm += url(loc('https://abc.net/sitemap.xml'), lastmod('2020-07-08T13:12:16+00:00'))

print(sm)
# <urlset><url><loc>https://abc.net/sitemap.xml</loc><lastmod>2020-07-08T13:12:16+00:00</lastmod></url></urlset>

Namespaced tags

There are a few options for creating namespaced tags such as image:image, image:loc, and image:caption.

Use globals() to get them by name, because : is not valid in Python variable names:

str(globals()["image:license"]())  # returns '<image:license></image:license>'

Or use create_ns_element:

from domonic.xml.sitemap import create_ns_element

vt = create_ns_element("video:title")
print(str(vt))

You can also use an underscore instead of a colon.

from domonic.xml.sitemap import *
print(geo_place_name())
# <geo:place_name></geo:place_name>

Formatting

You can format with the normal Python methods recognized by domonic:

print(f"{sm}")    # Calls __format__ and prettifies the XML.
print(f"{sm!s}")  # Calls str without prettifying.
print(f"{sm!r}")  # Shows the object repr.
# print(f"{sm!a}")

More

For more information on sitemaps, see:

https://www.sitemaps.org/protocol.html

domonic.sitemap

generate or load sitemaps

warning - when using image and video tags from this package they will be namespaced i.e <image:image> and <video:video> so i’d advise to only import them within the def that you use them in to avoid conflict with html.image

domonic.xml.sitemap.sitemap_format(self, *args, **kwargs)[source]

attempts to prettify the output of the sitemap.

class domonic.xml.sitemap.sitemapindex(*args, **kwargs)

Bases: Document

name: str = 'sitemapindex'
xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9'
class domonic.xml.sitemap.sitemap(*args, **kwargs)

Bases: Element

name: str = 'sitemap'
class domonic.xml.sitemap.urlset(*args, **kwargs)

Bases: Element

name: str = 'urlset'
xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9'
class domonic.xml.sitemap.url(*args, **kwargs)

Bases: Element

name: str = 'url'
class domonic.xml.sitemap.loc(*args, **kwargs)

Bases: Element

name: str = 'loc'
class domonic.xml.sitemap.lastmod(*args, **kwargs)

Bases: Element

name: str = 'lastmod'
class domonic.xml.sitemap.changefreq(*args, **kwargs)

Bases: Element

name: str = 'changefreq'
class domonic.xml.sitemap.priority(*args, **kwargs)

Bases: Element

name: str = 'priority'
domonic.xml.sitemap.sitemapindex_from_urls(urls)[source]

Create a sitemap index from a list of urls.

WARNING: there’s a difference between a sitemap index and a sitemap. make sure you know what you want.

# i.e

# <?xml version=”1.0” encoding=”UTF-8”?> # <sitemapindex xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9”> # <sitemap> # <loc>https://xyz.com/sitemap1.xml</loc> # <lastmod>2021-07-08T13:12:16+00:00</lastmod> # </sitemap> # </sitemapindex>

domonic.xml.sitemap.sitemap_from_urls(urls)[source]

Create a sitemap from a list of urls.add()

Note: This won’t allow you to add priority or changefreq of the urls. or add images etc tho u could loop the nodes afterwards and do that.

WARNING: there’s a difference between a sitemap index and a sitemap. make sure you know what you want.

domonic.xml.sitemap.get_sitemap(path, *args, **kwargs)[source]

Download a sitemap

domonic.xml.sitemap.create_ns_element(tag_name, **attributes)[source]

Factory function to create elements dynamically.

domonic.xml.sitemap.atom_link

alias of atom:link

domonic.xml.sitemap.geo_country

alias of geo:country

domonic.xml.sitemap.geo_geo

alias of geo:geo

domonic.xml.sitemap.geo_place_name

alias of geo:place_name

domonic.xml.sitemap.image_caption

alias of image:caption

domonic.xml.sitemap.image_geo_location

alias of image:geo_location

domonic.xml.sitemap.image_image

alias of image:image

domonic.xml.sitemap.image_license

alias of image:license

domonic.xml.sitemap.image_loc

alias of image:loc

domonic.xml.sitemap.image_title

alias of image:title

domonic.xml.sitemap.mobile_mobile

alias of mobile:mobile

domonic.xml.sitemap.news_keywords

alias of news:keywords

domonic.xml.sitemap.news_news

alias of news:news

domonic.xml.sitemap.news_publication_date

alias of news:publication_date

domonic.xml.sitemap.news_stock_tickers

alias of news:stock_tickers

domonic.xml.sitemap.news_title

alias of news:title

domonic.xml.sitemap.video_category

alias of video:category

domonic.xml.sitemap.video_content_loc

alias of video:content_loc

domonic.xml.sitemap.video_description

alias of video:description

domonic.xml.sitemap.video_duration

alias of video:duration

domonic.xml.sitemap.video_price

alias of video:price

domonic.xml.sitemap.video_price_currency

alias of video:price_currency

domonic.xml.sitemap.video_publication_date

alias of video:publication_date

domonic.xml.sitemap.video_rating

alias of video:rating

domonic.xml.sitemap.video_tags

alias of video:tags

domonic.xml.sitemap.video_thumbnail_loc

alias of video:thumbnail_loc

domonic.xml.sitemap.video_title

alias of video:title

domonic.xml.sitemap.video_video

alias of video:video

domonic.xml.sitemap.video_view_count

alias of video:view_count

domonic.xml.sitemap.xhtml_link

alias of xhtml:link