Site
A Site object represents a MediaWiki site with a particular hostname, such as the English Wikipedia with hostname en.wikipedia.org. Typical initialization of a Site:
site = mwclient.Site('en.wikipedia.org')
Do not include the leading http://
. By default Site assumes that api.php
is located at path /w/
(e.g. http://en.wikipedia.org/w/api.php). If the site you are connecting to places it in a different location, you must specify this (path must end in a /
):
site = mwclient.Site('conservapedia.com', path='/')
site = mwclient.Site('sourceforge.net', path='/apps/mediawiki/mwclient/')
Typically api.php
is in the same location as index.php
. If you edit or view source of a page, you should see the path of index.php
in the URL (e.g. http://en.wikipedia.org/w/index.php?title=Apple&action=edit).
By default, mwclient will allow you to view but not edit pages unless you are logged in. It is good practice to always log in to an account when editing using a bot, so that it can be blocked if it malfunctions:
site.login('username', 'password')
This will raise LoginError upon failure.
Site provides access to any page by name as a Page object via the Pages
object:
page = site.Pages['Apple']
template = site.Pages['Template:Stub']
wppage = site.Pages['Wikipedia:Five pillars']
mwpage = site.Pages['MediaWiki:Aboutsite']
image = site.Pages['Image:Wiki.png'] # This will return an [[Image]] object
category = site.Pages['Category:People'] # This will return a [[Category]] object
For images or categories, you can alternatively use the Images
or Categories
object and omit the namespace prefix:
image2 = site.Images['Wiki.png'] # The same image as above
category2 = site.Categories['People'] # The same category as above
- (constructor) _init_
- login
- allpages
- Site.alllinks
- Site.allcategories
- Site.allusers
- Site.blocks
- Site.deletedrevisions
- Site.email
- Site.expandtemplates
- Site.exturlusage
- Site.logevents
- Site.parse
- Site.random
- Site.recentchanges
- Site.require
- Site.search
- Site.upload
- Site.usercontributions
- Site.users
- Site.watchlist
- [[Site.repr|repr]]
- (static) Site._to_str
- (static) Site._query_string
- Site.handle_api_result
- Site.site_init
- Site.wait_token
- Site.wait
This page was originally imported from the old mwclient wiki at SourceForge. The imported version was dated from 09:10, 25 November 2011, and its only editor was Derrickcoetzee (@dcoetzee).
- Querying Wikipedia with mwclient
- Replacing a string for every page in a category
- Creating a page listing all pages in a category
Note: the red links below are pages yet to be created. Feel free to add them!