XML Sitemap Generator

Generate XML sitemaps from a list of URLs with priority and change frequency settings.

Enter one URL per line. HTTP/HTTPS required.

Часто задаваемые вопросы

Реализация кода

from xml.dom.minidom import parseString
from xml.etree.ElementTree import Element, SubElement, tostring
from datetime import date

def generate_sitemap(urls):
    urlset = Element("urlset")
    urlset.set("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")

    for entry in urls:
        url_el = SubElement(urlset, "url")
        SubElement(url_el, "loc").text = entry["url"]
        SubElement(url_el, "lastmod").text = entry.get("lastmod", date.today().isoformat())
        SubElement(url_el, "changefreq").text = entry.get("changefreq", "weekly")
        SubElement(url_el, "priority").text = str(entry.get("priority", 0.8))

    xml_str = tostring(urlset, encoding="unicode")
    declaration = '<?xml version="1.0" encoding="UTF-8"?>\n'
    return declaration + parseString(xml_str).toprettyxml(indent="  ").split("\n", 1)[1]

urls = [
    {"url": "https://example.com/", "priority": 1.0, "changefreq": "daily"},
    {"url": "https://example.com/about", "priority": 0.7},
    {"url": "https://example.com/contact", "priority": 0.5},
]
print(generate_sitemap(urls))

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.