Jump to content

API/Parsing wikitext

From Numinix MediaWiki Demo
Revision as of 17:02, 25 July 2026 by Admin (talk | contribs) (Seed from mediawiki.org for Seekmodo connector demo)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<languages/>

<translate> GET/POST request to parse content of a page and obtain the output.

API documentation[edit]

</translate>


<translate>

Example 1: Parse content of a page[edit]

GET request[edit]

</translate>


<translate>

Response[edit]

</translate> <syntaxhighlight lang="json"> {

   "parse": {
       "title": "Pet door",
       "pageid": 3276454,
       "revid": 852892138,
       "text": {

"*": "

<a href=\"/wiki/File:Doggy_door_exit.JPG\" class=\"image\"><img alt=\"\" src=\"//upload.wikimedia.org/wikipedia/commons/thumb/7/71/Doggy_door_exit.JPG/220px-Doggy_door_exit.JPG\" width=\"220\" height=\"165\" class=\"thumbimage\" srcset=\"//upload.wikimedia.org/wikipedia/commons/thumb/7/71/Doggy_door_exit.JPG/330px-Doggy_door_exit.JPG 1.5x,
           ...
       }
   }

} </syntaxhighlight>

<translate>

Sample code[edit]

</translate> /Sample code 1}}

<translate>

Example 2: Parse a section of a page and fetch its table data[edit]

GET request[edit]

</translate>


<translate>

Response[edit]

</translate>

<syntaxhighlight lang="json"> {

   "parse": {
       "title": "Wikipedia:Unusual articles/Places and infrastructure",
       "pageid": 38664530,
       "wikitext": {
           "*": "===Antarctica===\n\n{| class=\"wikitable\"\n|-\n| Emilio Palma\n| An Argentine national who is the first person known to be born on the continent of Antarctica.\n|-\n| Scouting in the Antarctic\n| Always be prepared for glaciers and penguins.\n|}"
       }
   }

} </syntaxhighlight>

<translate>

Sample code[edit]

</translate>

<syntaxhighlight lang="python3">

  1. !/usr/bin/python3

"""

   parse_wikitable.py
   MediaWiki Action API Code Samples
   Demo of `Parse` module: Parse a section of a page, fetch its table data and save
   it to a CSV file
   MIT license

"""

import csv import requests

S = requests.Session()

URL = "https://en.wikipedia.org/w/api.php"

TITLE = "Wikipedia:Unusual_articles/Places_and_infrastructure"

PARAMS = {

   'action': "parse",
   'page': TITLE,
   'prop': 'wikitext',
   'section': 5,
   'format': "json"

}


def get_table():

   """ Parse a section of a page, fetch its table data and save it to a CSV file
   """
   res = S.get(url=URL, params=PARAMS)
   data = res.json()
   wikitext = data['parse']['wikitext']['*']
   lines = wikitext.split('|-')
   entries = []
   for line in lines:
       line = line.strip()
       if line.startswith("|"):
           table = line[2:].split('||')
           entry = table[0].split("|")[0].strip("[[]]\n"), table[0].split("|")[1].strip("\n")
           entries.append(entry)
   file = open("places_and_infrastructure.csv", "w")
   writer = csv.writer(file)
   writer.writerows(entries)
   file.close()

if __name__ == '__main__':

   get_table()

</syntaxhighlight>

<translate>

Possible errors[edit]

</translate>

<translate> Code</translate> <translate> Info</translate>
missingtitle
nosuchsection
pagecannotexist
invalidparammix

<translate>

Parameter history[edit]

</translate>

  • v1.45: <translate> Introduced <tvar name=1>parser</tvar></translate>
  • v1.43: <translate> Introduced <tvar name=1>usearticle</tvar></translate>
  • v1.38: <translate> Introduced <tvar name=1>showstrategykeys</tvar></translate>
  • v1.32: <translate> Deprecated <tvar name=1>disabletidy</tvar></translate>
  • v1.31: <translate> Introduced <tvar name=1>disablestylededuplication</tvar></translate>
  • v1.30: <translate> Introduced <tvar name=1>revid, useskin, wrapoutputclass</tvar></translate>

<translate>

See also[edit]

</translate>


Sample content adapted from mediawikiwiki:API:Parsing_wikitext on mediawiki.org, licensed under CC BY-SA 4.0.