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.
 
 
 
 

41 line
1.4 KiB

  1. Integration
  2. ===========
  3. :mod:`mwparserfromhell` is used by and originally developed for EarwigBot_;
  4. :class:`~earwigbot.wiki.page.Page` objects have a
  5. :meth:`~earwigbot.wiki.page.Page.parse` method that essentially calls
  6. :func:`mwparserfromhell.parse() <mwparserfromhell.__init__.parse>` on
  7. :meth:`~earwigbot.wiki.page.Page.get`.
  8. If you're using Pywikibot_, your code might look like this::
  9. import mwparserfromhell
  10. import pywikibot
  11. def parse(title):
  12. site = pywikibot.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 any page using the following code
  17. (via the API_)::
  18. import json
  19. from urllib.parse import urlencode
  20. from urllib.request import urlopen
  21. import mwparserfromhell
  22. API_URL = "https://en.wikipedia.org/w/api.php"
  23. def parse(title):
  24. data = {"action": "query", "prop": "revisions", "rvlimit": 1,
  25. "rvprop": "content", "format": "json", "titles": title}
  26. raw = urlopen(API_URL, urlencode(data).encode()).read()
  27. res = json.loads(raw)
  28. text = res["query"]["pages"].values()[0]["revisions"][0]["*"]
  29. return mwparserfromhell.parse(text)
  30. .. _EarwigBot: https://github.com/earwig/earwigbot
  31. .. _Pywikibot: https://www.mediawiki.org/wiki/Manual:Pywikibot
  32. .. _API: http://mediawiki.org/wiki/API