API:Ewekansige
This page is part of the MediaWiki Action API documentation. |
MediaWiki version: | ≥ 1.12 |
Kry versoek om 'n lys ewekansige bladsye te sien.
Hierdie module kan as generator gebruik word.
API dokumentasie
Voorbeeld
Kry voersoek
Lys 5 ewekansige bladsye
Antwoord
{
"batchcomplete": "",
"continue": {
"rncontinue": "0.559881820010|0.559881954661|47659388|0",
"continue": "-||"
},
"query": {
"random": [
{
"id": 32381675,
"ns": 0,
"title": "Mallabhum Institute of Technology"
},
{
"id": 25126452,
"ns": 3,
"title": "User talk:96.232.132.176"
},
{
"id": 1440028,
"ns": 0,
"title": "Hyundai Epsilon engine"
},
{
"id": 35446805,
"ns": 15,
"title": "Category talk:Ukrainian card games"
},
{
"id": 12613,
"ns": 0,
"title": "Grue"
}
]
}
}
Voorbeeldkode
Python
#!/usr/bin/python3
"""
get_random.py
MediaWiki API Demos
Demo of `Random` module: Get request to list 5 random pages.
MIT License
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
"action": "query",
"format": "json",
"list": "random",
"rnlimit": "5"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
RANDOMS = DATA["query"]["random"]
for r in RANDOMS:
print(r["title"])
PHP
<?php
/*
get_random.php
MediaWiki API Demos
Demo of `Random` module: Get request to list 5 random pages.
MIT License
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
"action" => "query",
"format" => "json",
"list" => "random",
"rnlimit" => "5"
];
$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 );
foreach( $result["query"]["random"] as $k => $v ) {
echo( $v["title"] . "\n" );
}
JavaScript
/*
get_random.js
MediaWiki API Demos
Demo of `Random` module: Get request to list 5 random pages.
MIT License
*/
var url = "https://en.wikipedia.org/w/api.php";
var params = {
action: "query",
format: "json",
list: "random",
rnlimit: "5"
};
url = url + "?origin=*";
Object.keys(params).forEach(function(key){url += "&" + key + "=" + params[key];});
fetch(url)
.then(function(response){return response.json();})
.then(function(response) {
var randoms = response.query.random;
for (var r in randoms) {
console.log(randoms[r].title);
}
})
.catch(function(error){console.log(error);});
MediaWiki JS
/*
get_random.js
MediaWiki API Demos
Demo of `Random` module: Get request to list 5 random pages.
MIT License
*/
var params = {
action: 'query',
format: 'json',
list: 'random',
rnlimit: '5'
},
api = new mw.Api();
api.get( params ).done( function ( data ) {
var randoms = data.query.random,
r;
for ( r in randoms ) {
console.log( randoms[ r ].title );
}
} );
Paramter geskiedenis
- v1.26: Vorige limiet van 10/20 op
rnlimit
is verhoog tot standaard 500/5000 perke. - v1.26: Deprecated
rnredirect
- v1.26: Introduced
rnfilterredir
- v1.14: Introduced
rnredirect
Addisionele notas
- Ander as die meeste modules in die Action API ,is die standaardaantal teruggestuurde bladsye 1,nie 10 nie.
- Bladsye word in 'n vaste volgorde teruggestuur;slegs die beginpunt is eintlik lukraak.
- Die standaardgedrag is om bladsye van die hele wiki te kies, insluitend praatbladsye, gebruikersbladsye, ensovoorts. If you are looking for similar functionality as Special:Random, i.e. pick random articles, restrict
rnnamespace
to0
. - As die aantal bladsye minder as
rnlimit
is,sal die versoek alle beskikbare bladsye eenvoudig terugstuur. Dit sal nie bladsye herhaal om die antwoord tot die maksimum te beperk nie.
Sien ook
- Manual:Random page - Beskryf hoe u die API op die backend kan instel.