How to create in sitemap.xml?

Question:

I am not able create a sitemap with the following code?

from usp.tree import sitemap_tree_for_homepage

tree = sitemap_tree_for_homepage('')
print(tree)

for page in tree.all_pages():
    print(page)
    
Asked By: May

||

Answers:

The sitemap layout looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<urlset >this thread you can read how to create a xml file:

from usp.tree import sitemap_tree_for_homepage
import xml.etree.cElementTree as ET
import simplejson as json

tree = sitemap_tree_for_homepage('https://www.nytimes.com/')

root = ET.Element("urlset", bElement(urlel, "lastmod").text = lm
    ET.SubElement(urlel, "changefreq").text = cf
    ET.SubElement(urlel, "priority").text = prio

ET.indent(root, "  ") # pretty print
xmltree = ET.ElementTree(root)
xmltree.write("sitemap.xml", encoding="utf-8", xml_declaration=True )
    

If you want the lastmod to be todays date. Import date from datetime.

from datetime import date

and replace

page.last_modified.strftime("%Y-%m-%dT%H:%M%z")

with

date.today().strftime("%Y-%m-%dT%H:%M%z")

sitemap.xml

<?xml version='1.0' encoding='utf-8'?>
<urlset rel="nofollow noreferrer">https://www.example.com/ as your url you will not get the ouput above. Because example.com does not have a sitemap.xml. So use a different url.

Answered By: noah1400
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.