Skip to main content

MWParserFromHell is a parser for MediaWiki wikicode.

Project description

Build Status Coverage Status

mwparserfromhell (the MediaWiki Parser from Hell) is a Python package that provides an easy-to-use and outrageously powerful parser for MediaWiki wikicode. It supports Python 2 and Python 3.

Developed by Earwig with contributions from Σ, Legoktm, and others. Full documentation is available on ReadTheDocs. Development occurs on GitHub.


The easiest way to install the parser is through the Python Package Index; you can install the latest release with pip install mwparserfromhell (get pip). Make sure your pip is up-to-date first, especially on Windows.

Alternatively, get the latest development version:

git clone
cd mwparserfromhell
python install

You can run the comprehensive unit testing suite with python test -q.


Normal usage is rather straightforward (where text is page text):

>>> import mwparserfromhell
>>> wikicode = mwparserfromhell.parse(text)

wikicode is a mwparserfromhell.Wikicode object, which acts like an ordinary str object (or unicode in Python 2) with some extra methods. For example:

>>> text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?"
>>> wikicode = mwparserfromhell.parse(text)
>>> print(wikicode)
I has a template! {{foo|bar|baz|eggs=spam}} See it?
>>> templates = wikicode.filter_templates()
>>> print(templates)
>>> template = templates[0]
>>> print(
>>> print(template.params)
['bar', 'baz', 'eggs=spam']
>>> print(template.get(1).value)
>>> print(template.get("eggs").value)

Since nodes can contain other nodes, getting nested templates is trivial:

>>> text = "{{foo|{{bar}}={{baz|{{spam}}}}}}"
>>> mwparserfromhell.parse(text).filter_templates()
['{{foo|{{bar}}={{baz|{{spam}}}}}}', '{{bar}}', '{{baz|{{spam}}}}', '{{spam}}']

You can also pass recursive=False to filter_templates() and explore templates manually. This is possible because nodes can contain additional Wikicode objects:

>>> code = mwparserfromhell.parse("{{foo|this {{includes a|template}}}}")
>>> print(code.filter_templates(recursive=False))
['{{foo|this {{includes a|template}}}}']
>>> foo = code.filter_templates(recursive=False)[0]
>>> print(foo.get(1).value)
this {{includes a|template}}
>>> print(foo.get(1).value.filter_templates()[0])
{{includes a|template}}
>>> print(foo.get(1).value.filter_templates()[0].get(1).value)

Templates can be easily modified to add, remove, or alter params. Wikicode objects can be treated like lists, with append(), insert(), remove(), replace(), and more. They also have a matches() method for comparing page or template names, which takes care of capitalization and whitespace:

>>> text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}"
>>> code = mwparserfromhell.parse(text)
>>> for template in code.filter_templates():
...     if"Cleanup") and not template.has("date"):
...         template.add("date", "July 2012")
>>> print(code)
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}}
>>> code.replace("{{uncategorized}}", "{{bar-stub}}")
>>> print(code)
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}}
>>> print(code.filter_templates())
['{{cleanup|date=July 2012}}', '{{bar-stub}}']

You can then convert code back into a regular str object (for saving the page!) by calling str() on it:

>>> text = str(code)
>>> print(text)
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}}
>>> text == code

Likewise, use unicode(code) in Python 2.


An inherent limitation in wikicode prevents us from generating complete parse trees in certain cases. For example, the string {{echo|''Hello}}, world!'' produces the valid output <i>Hello, world!</i> in MediaWiki, assuming {{echo}} is a template that returns its first parameter. But since representing this in mwparserfromhell’s node tree would be impossible, we compromise by treating the first node (i.e., the template) as plain text, parsing only the italics.

The current workaround for cases where you are not interested in text formatting is to pass skip_style_tags=True to mwparserfromhell.parse(). This treats '' and ''' like plain text.

A future version of mwparserfromhell will include multiple parsing modes to get around this restriction.


mwparserfromhell is used by and originally developed for EarwigBot; Page objects have a parse method that essentially calls mwparserfromhell.parse() on page.get().

If you’re using Pywikibot, your code might look like this:

import mwparserfromhell
import pywikibot

def parse(title):
    site = pywikibot.Site()
    page = pywikibot.Page(site, title)
    text = page.get()
    return mwparserfromhell.parse(text)

If you’re not using a library, you can parse any page using the following Python 3 code (via the API):

import json
from urllib.parse import urlencode
from urllib.request import urlopen
import mwparserfromhell
API_URL = ""

def parse(title):
    data = {"action": "query", "prop": "revisions", "rvlimit": 1,
            "rvprop": "content", "format": "json", "titles": title}
    raw = urlopen(API_URL, urlencode(data).encode()).read()
    res = json.loads(raw)
    text = res["query"]["pages"].values()[0]["revisions"][0]["*"]
    return mwparserfromhell.parse(text)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
mwparserfromhell-0.4.4-cp27-cp27m-win32.whl (90.1 kB) Copy SHA256 hash SHA256 Wheel cp27
mwparserfromhell-0.4.4-cp27-cp27m-win_amd64.whl (92.0 kB) Copy SHA256 hash SHA256 Wheel cp27
mwparserfromhell-0.4.4-cp33-cp33m-win32.whl (90.5 kB) Copy SHA256 hash SHA256 Wheel cp33
mwparserfromhell-0.4.4-cp33-cp33m-win_amd64.whl (92.3 kB) Copy SHA256 hash SHA256 Wheel cp33
mwparserfromhell-0.4.4-cp34-cp34m-win32.whl (90.5 kB) Copy SHA256 hash SHA256 Wheel cp34
mwparserfromhell-0.4.4-cp34-cp34m-win_amd64.whl (92.3 kB) Copy SHA256 hash SHA256 Wheel cp34
mwparserfromhell-0.4.4-cp35-cp35m-win32.whl (93.5 kB) Copy SHA256 hash SHA256 Wheel cp35
mwparserfromhell-0.4.4-cp35-cp35m-win_amd64.whl (96.9 kB) Copy SHA256 hash SHA256 Wheel cp35
mwparserfromhell-0.4.4.tar.gz (118.4 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page