A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637
  1. Integration
  2. ===========
  3. :py:mod:`mwparserfromhell` is used by and originally developed for EarwigBot_;
  4. :py:class:`~earwigbot.wiki.page.Page` objects have a
  5. :py:meth:`~earwigbot.wiki.page.Page.parse` method that essentially calls
  6. :py:func:`mwparserfromhell.parse() <mwparserfromhell.__init__.parse>` on
  7. :py:meth:`~earwigbot.wiki.page.Page.get`.
  8. If you're using Pywikipedia_, your code might look like this::
  9. import mwparserfromhell
  10. import wikipedia as pywikibot
  11. def parse(title):
  12. site = pywikibot.getSite()
  13. page = pywikibot.Page(site, title)
  14. text = page.get()
  15. return mwparserfromhell.parse(text)
  16. If you're not using a library, you can parse any page using the following code
  17. (via the API_)::
  18. import json
  19. import urllib
  20. import mwparserfromhell
  21. API_URL = "http://en.wikipedia.org/w/api.php"
  22. def parse(title):
  23. raw = urllib.urlopen(API_URL, data).read()
  24. res = json.loads(raw)
  25. text = res["query"]["pages"].values()[0]["revisions"][0]["*"]
  26. return mwparserfromhell.parse(text)
  27. .. _EarwigBot: https://github.com/earwig/earwigbot
  28. .. _Pywikipedia: https://www.mediawiki.org/wiki/Manual:Pywikipediabot
  29. .. _API: http://mediawiki.org/wiki/API