Jump to content
MediaWiki

API:Parsing wikitext/Sample code 1

From mediawiki.org

Python

[edit ]
#!/usr/bin/python3
"""
 parse.py
 MediaWiki API Demos
 Demo of `Parse` module: Parse content of a page
 MIT License
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
 "action": "parse",
 "page": "Pet door",
 "format": "json"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
print(DATA["parse"]["text"]["*"])

PHP

[edit ]
<?php
/*
 parse.php
 MediaWiki API Demos
 Demo of `Parse` module: Parse content of a page
 MIT License
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
 "action" => "parse",
 "page" => "Pet door",
 "format" => "json"
];
$url = $endPoint . "?" . http_build_query( $params );
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );
$result = json_decode( $output, true );
echo( $result["parse"]["text"]["*"] );

JavaScript

[edit ]
/**
 * parse.js
 *
 * MediaWiki API Demos
 * Demo of `Parse` module: Parse content of a page
 *
 * MIT License
 */

consturl="https://en.wikipedia.org/w/api.php?"+
newURLSearchParams({
origin:"*",
action:"parse",
page:"Pet door",
format:"json",
});
try{
constreq=awaitfetch(url);
constjson=awaitreq.json();
console.log(json.parse.text["*"]);
}catch(e){
console.error(e);
}

MediaWiki JS

[edit ]
/**
 * parse.js
 *
 * MediaWiki API Demos
 * Demo of `Parse` module: Parse content of a page
 * MIT License
 */
constparams={
action:'parse',
page:'Pet door',
format:'json'
};
constapi=newmw.Api();
api.get(params).done(data=>{
console.log(data.parse.text['*']);
});

AltStyle によって変換されたページ (->オリジナル) /