MWParserFromHell

0.7.2 · active · verified Sat Apr 11

MWParserFromHell is a Python package that provides an easy-to-use and outrageously powerful parser for MediaWiki wikicode. It supports Python 3.9+ and is actively developed with frequent releases to support new Python versions and address parsing nuances, typically releasing a few times a year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic parsing of MediaWiki wikicode, extracting templates, their parameters, and headings using the `mwparserfromhell.parse()` function and `Wikicode` object's filter methods.

import mwparserfromhell

text = """I has a template! {{foo|bar|baz|eggs=spam}} \n== Heading ==\n[[File:Example.jpg|thumb|A caption.]] See it?"""
wikicode = mwparserfromhell.parse(text)

print(wikicode) # Outputs the original wikicode

# Filter for templates
templates = wikicode.filter_templates()
if templates:
    template = templates[0]
    print(f"Template name: {template.name}")
    print(f"Template parameter '1': {template.get(1).value}")
    print(f"Template parameter 'eggs': {template.get('eggs').value}")

# Filter for wikilinks (e.g., file captions are wikilinks)
wikilinks = wikicode.filter_wikilinks()
if wikilinks:
    print(f"First wikilink: {wikilinks[0].title}")

# Get all headings
headings = wikicode.filter_headings()
if headings:
    print(f"First heading: {headings[0].title}")

view raw JSON →