API:カテゴリ

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

Other languages:
English • ‎dansk • ‎polski • ‎русский • ‎日本語
MediaWiki バージョン: 1.11

ページまたはページ群に関連付けられたカテゴリ群 を閲覧する GET リクエストです。

This module can be used as a generator .

APIの説明文書


prop=categories (cl)

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

List all categories the pages belong to.

Parameters:
clprop

Which additional properties to get for each category:

sortkey
Adds the sortkey (hexadecimal string) and sortkey prefix (human-readable part) for the category.
timestamp
Adds timestamp of when the category was added.
hidden
Tags categories that are hidden with __HIDDENCAT__.
Values (separate with | or alternative): sortkey, timestamp, hidden
clshow

Which kind of categories to show.

Values (separate with | or alternative): hidden, !hidden
cllimit

How many categories to return.

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

When more results are available, use this to continue.

clcategories

Only list these categories. Useful for checking whether a certain page is in a certain category.

Separate values with | or alternative. Maximum number of values is 50 (500 for bots).
cldir

The direction in which to list.

One of the following values: ascending, descending
Default: ascending
Examples:
Get a list of categories the page Albert Einstein belongs to.
api.php?action=query&prop=categories&titles=Albert%20Einstein [open in sandbox]
Get information about all categories used in the page Albert Einstein.
api.php?action=query&generator=categories&titles=Albert%20Einstein&prop=info [open in sandbox]

GET リクエスト

ページが属するカテゴリ群を閲覧する GET リクエストです。

レスポンス

{
    "continue": {
        "clcontinue": "13828397|Afrofuturists",
        "continue": "||"
    },
    "query": {
        "pages": {
            "13828397": {
                "pageid": 13828397,
                "ns": 0,
                "title": "Janelle Mon\u00e1e",
                "categories": [
                    {
                        "ns": 14,
                        "title": "Category:1985 births"
                    },
                    {
                        "ns": 14,
                        "title": "Category:21st-century American actresses"
                    },
                    {
                        "ns": 14,
                        "title": "Category:21st-century American singers"
                    },
                    ...
                ]
            }
        }
    }
}

サンプル コード

get_categories.py

#!/usr/bin/python3

"""
    get_categories.py

    MediaWiki Action API Code Samples
    Demo of `Categories` module: GET request to view categories
    associated with the Janelle Monáe page.
    MIT license
"""

import requests

S = requests.Session()

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

PARAMS = {
    "action":"query",
    "format":"json",
    "titles":"Janelle Monáe",
    "prop":"categories",
}

R = S.get(url=URL, params=PARAMS)
DATA = R.json()

print(DATA)

起こりうるエラー

コード 情報
clshow Incorrect parameter - mutually exclusive values may not be supplied.

パラメーターの履歴

  • v1.20: cldir を導入しました
  • v1.16: clprop=hidden を導入しました
  • v1.15: clcategories を導入しました
  • v1.14: clshow を導入しました
  • v1.13: clcontinue, cllimit, clprop=timestamp を導入しました

関連項目