API:随机

From MediaWiki.org
Jump to navigation Jump to search
This page is a translated version of the page API:Random and the translation is 70% complete.

Other languages:
English • ‎català • ‎dansk • ‎español • ‎français • ‎polski • ‎română • ‎русский • ‎ไทย • ‎中文 • ‎日本語 • ‎한국어
MediaWiki版本: 1.12

GET请求查看随机页面列表。

该模块可用作生成器

API文档


list=random (rn)

(main | query | random)
  • This module requires read rights.
  • This module can be used as a generator.
  • Source: MediaWiki
  • License: GPL-2.0-or-later

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.

Parameters:
rnnamespace

Return pages in these namespaces only.

Values (separate with | or alternative): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 90, 91, 92, 93, 100, 101, 102, 103, 104, 105, 106, 107, 486, 487, 828, 829, 1198, 1199, 2300, 2301, 2302, 2303, 2600, 5500, 5501
To specify all values, use *.
rnfilterredir

How to filter for redirects.

One of the following values: all, redirects, nonredirects
Default: nonredirects
rnredirect
Deprecated.

Use rnfilterredir=redirects instead.

Type: boolean (details)
rnlimit

Limit how many random pages will be returned.

No more than 500 (5,000 for bots) allowed.
Type: integer or max
Default: 1
rncontinue

When more results are available, use this to continue.

Examples:
Return two random pages from the main namespace.
api.php?action=query&list=random&rnnamespace=0&rnlimit=2 [open in sandbox]
Return page info about two random pages from the main namespace.
api.php?action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info [open in sandbox]

示例

GET请求

列出5个随机页面

响应

{

"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"
            }
        ]
    }
}

示例代码

get_random.py

#!/usr/bin/python3

"""
    get_random.py

    MediaWiki Action API Code Samples
    Demo of `Random` module: Get a list of 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()

print(DATA)

参数历史

  • v1.26: Previous limit of 10/20 on rnlimit was increased to standard 500/5000 limits.
  • v1.26: 棄用rnredirect
  • v1.26: 啟用rnfilterredir
  • v1.14: 啟用rnredirect


附加提醒

  • Unlike most modules in the Action API , the default number of pages returned is 1, not 10.
  • Pages are returned in a fixed sequence; only the starting point is actually random.
  • The default behavior is to pick pages from across the entire wiki, including talk pages, user pages, and so on. If you are looking for similar functionality as Special:Random, i.e. pick random articles, restrict rnnamespace to 0.
  • If the number of pages is fewer than rnlimit, the request will simply return all available pages. It will not repeat pages to pad out the response up to the limit.

参见