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)
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.
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)
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.