MediaWiki Quickstart

Quickly get started using the mediawiki python library. This page is designed to help users understand the basics of using the mediawiki library.

To understand all possible parameters for each function and properties, please see MediaWiki Documentation.


Using pip

$ pip install pymediawiki

From source

Begin by installing pymediawiki: simply clone the repository on GitHub, then run the following command from the extracted folder:

$ python install


Setting up the library is as easy as:

>>> from mediawiki import MediaWiki
>>> wikipedia = MediaWiki()

Change API URL

To change the API URL, one can either set the url parameter

>>> from mediawiki import MediaWiki
>>> asoiaf = MediaWiki(url='')

Or one can update an already setup MediaWiki object:

>>> wikipedia.set_api_url('')

Set the User-Agent String

Per the MediaWiki API Etiquette it is recommended to not use a library’s default user-agent string. Therefore, it is easy to change the user-agent string either during initialization or by setting the user_agent property:

>>> from mediawiki import MediaWiki
>>> wikipedia = MediaWiki(user_agent='pyMediaWiki-User-Agent-String')
>>> # Or reset it!
>>> wikipedia.user_agent = 'my-new-user-agent-string'


To search the MediaWiki site, it is as easy as calling one of the search functions: random, search, geosearch, opensearch, or prefixsearch


Get a random page:

>>> wikipedia.random(pages=3)
# ['Sutton House, London', 'Iolaus violacea', 'Epigenetics & Chromatin']


Search for the provided title:

>>> wikipedia.allpages('a', results=3)
# ['A', 'A!', 'A! (Alexa Feser album)']


Search based on geocoords (latitude/longitude):

>>> wikipedia.geosearch(latitude=0.0, longitude=0.0)
# ['Null Island', 'Mirdif 35']


Search using the OpenSearch specification:

>>> wikipedia.opensearch('new york', results=1)
# [('New York', 'New York is a state in the Northeastern United States
and is the 27th-most extensive, fourth-most populous, and seventh-most
densely populated U.S.', '')]


Search for pages whose title has the defined prefix:

>>> wikipedia.prefixsearch('ba', results=5)
# ['Ba', 'Barack Obama', 'Baseball', "Bahá'í Faith", 'Basketball']


Load and access information from full MediaWiki pages. Load the page using a title or page id and then access individual properties:

Initialize Page

Initializing a page is easily accomplished in one line of code

>>> p ='grid compass')


The page title

>>> p.title
# 'Grid Compass'


The page id of the page

>>> p.pageid
# 3498511


The revision id of the page

>>> p.revision_id
# 740685101


The parent id of the page

>>> p.parent_id
# 740682666

Other Properties

Other properties for a page include:

  • content
  • html
  • images
  • references
  • categories
  • coordinates
  • redirects
  • backlinks
  • langlinks
  • summary
  • sections
  • logos
  • hatnotes


Summarize a page using additional parameters:

>>> p.summarize(chars=50)
# The Grid Compass (written GRiD by its manufacturer...