1

I am trying to create a sitemap for my website instead of having to run it through a website that will make it for me. This is because the website changes quite often.

I found code online that achieves part of it:

@app.route('/sitemap.xml', methods=['GET'])
def sitemap():
    try:
      """Generate sitemap.xml. Makes a list of urls and date modified."""
      pages = []
      seven_days_ago = (datetime.datetime.now() - datetime.timedelta(days=7)).date().isoformat()
      for rule in app.url_map.iter_rules():
          if "GET" in rule.methods and len(rule.arguments) == 0:
               pages.append( ["..." + str(rule.rule), seven_days_ago])
 
      sitemap_xml = render_template('pages/sitemap_template.xml', pages=pages)
      response = make_response(sitemap_xml)
      response.headers["Content-Type"] = "application/xml"

      return response
    except Exception as e:
        return(str(e))

It works to create a basic sitemap. Okay, easy enough.

I want to add a priority in the meta tags of each page and then build the sitemap off of that. This SO question/answer covers that but it is using beautifulsoup and urllib, and is geared more toward the web, not a local instance.

So, I figure I need to render_template for each route (in this case, rule.rule) then parse that, maybe with BeautifulSoup and get the priority. I have no idea how to do this. Is there a way to GET each template per its route so I can parse it?

David Frick
  • 641
  • 1
  • 9
  • 25

1 Answers1

0

I ended up using beautifulsoup4 on those routes. Shame that flask does not seem to offer a more direct way of this.

David Frick
  • 641
  • 1
  • 9
  • 25