Skip to content

Tutorial:Creating a page listing all pages in a category

Waldir Pimenta edited this page Mar 25, 2015 · 3 revisions

Suppose you wish to create a user subpage, [[User:Me/apples]], containing a bulleted list of all pages in the category "Apples", including links. The following code does this:

import mwclient
site = mwclient.Site(('https', 'en.wikipedia.org'))
site.login('username', 'password')
listpage = site.Pages['User:Me/apples']
text = listpage.text()
for page in site.Categories['Apples']:
    text += "* [[:" + page.name + "]]\n"
listpage.save(text, summary='Creating list from [[Category:Apples]]')

The variable "listpage" contains the subpage that the list will go on. We start editing it before the loop and save the result to it after the loop. The loop goes through all pages in [[Category:Apples]] and adds a line to the text of the page for each one. We include a : after the [[ so that subcategories and files will appear as links, rather than placing the page in those categories/embedding the files. Typical output looks like this:

If we want to include all pages in all subcategories, we can use a recursive function to walk the tree:

def listpages(category):
    text = ''
    for page in category:
        if page.namespace == 14:  # 14 is the category namespace
            text += listpages(page)
        else:
            text += "* [[:" + page.[[Page.name|name]] + "]]\n"
    return text

The for loop in our original example is replaced with a call to this function:

text += listpages(site.[[Site.Categories|Categories]]['Apples'])

A slightly more advanced version of listpages can produce a hierarchical list, with indentation reflecting the category structure:

def listpages(category, level = 1):
    text = <nowiki>''</nowiki>
    for page in category:
        text += "*" * level + <nowiki>" [[:"</nowiki> + page.[[Page.name|name]] + <nowiki>"]]\n"</nowiki>
        if page.[[Page.namespace|namespace]] == 14:  # 14 is the category namespace
            text += listpages(page, level+1)
    return text

The code "*" * level uses string multiplication to produce level stars. Output looks like this:


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