User:Didicodes/Sandbox/API:Random
| This page is part of the MediaWiki Action API documentation. |
| MediaWiki version: | ≥ 1.12 |
GET Request to get a list of random pages.
API documentation
[edit]
list=random (rn)
Get a set of random pages. Pages are listed in a fixed sequence, only the starting point is random. This means that if, for example, Main Page is the first random page in the list, List of fictional monkeys will always be second, List of people on stamps of Vanuatu third, etc. Specific parameters: Other general parameters are available.
Examples:
|
Examples
[edit]Example 1: Get two random pages
[edit]GET Request
[edit]Response
[edit]{
"batchcomplete": "",
"continue": {
"rncontinue": "0.613060281177|0.6130607018929999|3737740|0",
"continue": "-||"
},
"query": {
"random": [
{
"id": 22841705,
"ns": 0,
"title": "How We Decide"
},
{
"id": 6705281,
"ns": 0,
"title": "RRH Portreath"
}
]
}
}
Sample code
[edit]get_two_random_pages.py
#!/usr/bin/python3
"""
get_two_random_pages.py
MediaWiki Action API Code Samples
Demo of `Random` module: get two random pages
MIT license
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
"action": "query",
"format": "json",
"list": "random",
"rnnamespace": "0",
"rnlimit": "2"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.text
print(DATA)
Example 2: Get random pages
[edit]GET Request
[edit]Response
[edit]{
"batchcomplete": "",
"continue": {
"grncontinue": "0.215787713771|0.215787863486|44399457|0",
"continue": "grncontinue||"
},
"query": {
"pages": {
"52897381": {
"pageid": 52897381,
"ns": 1,
"title": "Talk:Dicarpellum",
"revisions": [
{
"revid": 760466166,
"parentid": 0,
"user": "Plantdrew",
"timestamp": "2017-01-17T04:15:53Z",
"comment": "[[WP:AES|\u2190]]Created page with '{{WikiProject Plants|class=stub|importance=low|needs-image=yes}}'"
}
],
"images": [
{
"ns": 6,
"title": "File:Camera-photo Upload.svg"
},
{
"ns": 6,
"title": "File:Chamomile@original size.jpg"
},
{
"ns": 6,
"title": "File:Rose Amber Flush 20070601.jpg"
},
{
"ns": 6,
"title": "File:Symbol stub class.svg"
}
]
}
}
}
}
Sample code
[edit]get_random_pages.py
#!/usr/bin/python3
"""
get_random_pages.py
MediaWiki Action API Code Samples
Demo of `Random` module: get list of two or more random pages by specifying rnlimit
MIT license
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
"action": "query",
"format": "json",
"prop": "revisions|images",
"generator": "random"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.text
print(DATA)
Parameter history
[edit]- v1.14: Introduced
rnredirect - v1.26: Deprecated
rnredirect - v1.26: Introduced
rnfilterredir