Waldir Pimenta edited this page May 31, 2014 · 2 revisions
Clone this wiki locally

Site.Pages is a PageList object that provides access to the Page object for any page on a site using its full name, as in:

page = site.Pages['Apple']
text = page.[[Page.edit|edit]]()

Pages outside the main namespace can be accessed using their usual namespace prefix (as in "Category:Apples", "File:Apple.jpg", or "Wikipedia:Articles for deletion"). When accessing a category or file description page, Pages automatically returns an object of type Category or Image, respectively.

No error is returned if the page does not exist; instead a Page object is created which can be used to create a new page using To test if a page exists, check the "exists" data member of the returned Page:

page = site.Pages['Nonexistent page']
if not page.[[Page.exists|exists]]:
    print 'Page not found, creating new page'
    page.[[|save]]('Text for new page', summary='Creating new page')

Or more succinctly:

if not site.Pages['Nonexistent page'].[[Page.exists|exists]]:
    print 'Page missing!'

Pages also permits iteration over all pages on a site, the same set of pages listed on-wiki at "Special:Allpages":

for page in site.Pages:
    print page.[[Page.page_title|page_title]]

For a large site, this kind of loop can take a long time to run.

The Page.Categories and Page.Images objects provide access to categories and files, respectively, without the need for the "Category:" or "File:" namespace prefix.

This page was originally imported from the old mwclient wiki at SourceForge. The imported version was dated from 11:18, 25 November 2011, and its only editor was Derrickcoetzee (@dcoetzee).