Table of Contents

How To Generate Google Sitemap Using MongoDB and Python

In this post, I will discuss about generating sitemap with the help of mongoDB. For that, I am assuming you will have all the URLs stored in the mongoDB.

I will use pymongo to query mongoDB. If you dont' have it. Install it using pip

pip install pymongo

Lets initiate the connection to mongoDB using pymongo now

from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017')
db = client['alternatedb'] 

I have my URLS stored as slugs in the following collection...

{'_id': ObjectId('5d518ca585468955cd403cb2'),
 'iosapp_id': 'id1016307224',
 'iosapp_slug': 'ios-app-Shark-Fishing-Extreme-Games-Free'}

Lets create the header first of sitemap.xml

header = '''<?xml version="1.0" encoding="UTF-8"?> 
<urlset \

Lets loop through the collection to create sitemap.xml now...

with open('sitemap.xml','w') as fp:
  for doc in db.iosapp_slugs.find({}):
    slug = doc['iosapp_slug']

If you have lot of URLS in mongo, you might run in to following error...

"errmsg": "Cursor not found, cursor id: ###",

To fix that add the batch_size to mongoDB loop command as shown below...

for doc in db.iosapp_slugs.find({}).batch_size(100):

If you are using Python, You might be using Django, Here is how to serve sitemap.xml in Django

from django.views.generic import TemplateView

Then in urlpatterns list add following...

url(r'^sitemap\.xml/$', TemplateView.as_view(template_name="sitemap.xml", content_type='text/xml'))

Related Posts