sitemap

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

A sitemap contains a list of urls for your website. Whereas a sitemap index contains a list of sitemaps.

You can see below How to make sitemaps with python and domonic.

creating a sitemapindex

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 number ways depending on our needs.

from domonic.xml.sitemap import sitemapindex, sitemap, url, loc, lastmod, changefreq, priority

doc = sitemapindex(
        sitemap(
                loc(https://xyz.net/sitemap1.xml)
                lastmod('2021-07-08T13:12:16+00:00')  # pass a date as string. if no data is passed the current date is used
)
)

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

Create a sitemap

A sitemap contains a list of 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 number ways depending on our needs.

from domonic.xml.sitemap import sitemapindex, sitemap, url, loc, lastmod, changefreq, priority

doc = urlset(
        url(
                loc('https://xyz.net')
                lastmod('2021-07-08T13:12:16+00:00')  # pass a date as string. if no data is passed the current date is used
                changefreq('weekly')
                priority(0.5)
        )
)

# use f-string to call format on the doc to prettify
render(f"{doc}", 'sitemap1.xml')

utils

domonic also has some utils for quickly creating sitemaps with default values.

mypages = []
sm = sitemap_from_urls(mypages)
print(sm)

but you will likely want a little more control. So use any dom manipulating methods you like.

Here’s some more examples.

## creating a sitemap from scratch

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

print(sm)

Namespaced tags

There’s a few options for creating the namespaced tags i.e. image:image, image:loc, <image:caption> etc

Call globals to get them by a name string due to colon not being valid in python variable names . i.e.

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

or there is a utility method to create them called create_ns_element:

    from domonic.xml.sitemap import create_ns_element

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

or you can use an underscore instead of a colon.

    from domonic.xml.sitemap import *
print(geo_placename())

formatting

You can format with following normal python methods which are regonised by domonic:

print(f"{sm}") # f string will call __format__ and run through a prettify
print(f"{sm!s}") # str will not be prettified
print(f"{sm!r}") # r show the object as a repr
# print(f"{sm!a}")

more

For more info 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.atom_link

alias of atom:link

class domonic.xml.sitemap.changefreq(*args, **kwargs)
domonic.xml.sitemap.create_ns_element(tag_name, **attributes)[source]

Factory function to create elements dynamically.

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.get_sitemap(path: str, *args, **kwargs)[source]

Download a sitemap

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

class domonic.xml.sitemap.lastmod(*args, **kwargs)
class domonic.xml.sitemap.loc(*args, **kwargs)
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

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

attempts to prettify the output of the sitemap.

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.

class domonic.xml.sitemap.sitemapindex(*args, **kwargs)
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>

class domonic.xml.sitemap.url(*args, **kwargs)
class domonic.xml.sitemap.urlset(*args, **kwargs)
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