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.
 
 
 
 

36 lines
1.2 KiB

  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.get_site()
  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 templates in any page using the
  17. following code (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: http://pywikipediabot.sourceforge.net/
  29. .. _API: http://mediawiki.org/wiki/API