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.
 
 
 
 

51 lines
1.7 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 with the following
  17. Python 3 code (using the API_ and the requests_ library):
  18. import requests
  19. import mwparserfromhell
  20. API_URL = "https://en.wikipedia.org/w/api.php"
  21. def parse(title):
  22. params = {
  23. "action": "query",
  24. "prop": "revisions",
  25. "rvprop": "content",
  26. "rvslots": "main",
  27. "rvlimit": 1,
  28. "titles": title,
  29. "format": "json",
  30. "formatversion": "2",
  31. }
  32. headers = {"User-Agent": "My-Bot-Name/1.0"}
  33. req = requests.get(API_URL, headers=headers, params=params)
  34. res = req.json()
  35. revision = res["query"]["pages"][0]["revisions"][0]
  36. text = revision["slots"]["main"]["content"]
  37. return mwparserfromhell.parse(text)
  38. .. _EarwigBot: https://github.com/earwig/earwigbot
  39. .. _Pywikibot: https://www.mediawiki.org/wiki/Manual:Pywikibot
  40. .. _API: https://www.mediawiki.org/wiki/API:Main_page
  41. .. _requests: https://2.python-requests.org