Skip to content
Waldir Pimenta edited this page Nov 16, 2013 · 1 revision

Functions which take a namespace argument in mwclient, as well as data members such as Page.namespace, express it in the form of an integer, the ''namespace index''. Built-in standard MediaWiki namespaces, including the article, user, project, file, template, category, help, and MediaWiki namespaces and their associated discussion pages, have fixed indexes associated with them, and are listed at Manual:Namespace.

A particular site may define custom namespaces. To list all namespaces on a site and their associated indexes, you can examine Site.namespaces using this code fragment:

site = mwclient.Site('en.wikipedia.org')
print site.namespaces

Given a namespace index, the name can be retrieved by indexing into Site.namespaces:

page = site.Pages['Portal talk:Contents']
print site.namespaces[page.namespace]        # Prints 'Portal talk'

By inverting this mapping, you can also easily refer to namespaces by name, as in this sample which lists all pages in the Portal talk namespace (a custom namespace on Wikipedia):

nsindexes = dict((v,k) for k,v in site.namespaces.iteritems())
for page in site.allpages(namespace=nsindexes['Portal talk']):
    print page.name

This page was originally imported from the old mwclient wiki at SourceForge. The imported version was dated from 00:50, 18 March 2012, and its only editor was Derrickcoetzee (@dcoetzee).