API:Kategorien

This page is a translated version of the page API:Categories and the translation is 100% complete.
MediaWiki Version:
1.11

GET-Abfrage um die Kategorien zu sehen, die einer oder mehreren Seiten zugeordnet sind.

Dieses Modul kann als Generator benutzt werden.

API-Dokumentation


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.

Specific parameters:
Other general parameters are available.
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): hidden, sortkey, timestamp
clshow

Which kind of categories to show.

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

How many categories to return.

Type: integer or max
The value must be between 1 and 500.
Default: 10
clcontinue

When more results are available, use this to continue. More detailed information on how to continue queries can be found on mediawiki.org.

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 clients that are allowed higher limits).
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]

Beispiel

GET-Anfrage

GET-Abfrage um die Kategorien einer Seite zu sehen.


Antwort

{
    "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"
                    },
                    ...
                ]
            }
        }
    }
}

Beispielcode

Python

#!/usr/bin/python3

"""
    get_categories.py

    MediaWiki API Demos
    Demo of `Categories` module: Get categories associated with a page.

    MIT License
"""

import requests

S = requests.Session()

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

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

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

PAGES = DATA["query"]["pages"]

for k, v in PAGES.items():
    for cat in v['categories']:
        print(cat["title"])

PHP

<?php
/*
    get_categories.php

    MediaWiki API Demos
    Demo of `Categories` module: Get categories associated with a page.

    MIT License
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "format" => "json",
    "prop" => "categories",
    "titles" => "Janelle Monáe"
];

$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"]["pages"] as $k => $v ) {
    foreach( $v["categories"] as $k => $v ) {
        echo( $v["title"] . "\n" );
    }
}

JavaScript

/*
    get_categories.js

    MediaWiki API Demos
    Demo of `Categories` module: Get categories associated with a page.

    MIT License
*/

var url = "https://en.wikipedia.org/w/api.php"; 

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

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 pages = response.query.pages;
        for (var p in pages) {
            for (var cat of pages[p].categories) {
                console.log(cat.title);
            }
        }
    })
    .catch(function(error){console.log(error);});

MediaWiki JS

/*
	get_categories.js

	MediaWiki API Demos
	Demo of `Categories` module: Get categories associated with a page.

	MIT License
*/

var params = {
		action: 'query',
		format: 'json',
		prop: 'categories',
		titles: 'Janelle Monáe'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	var pages = data.query.pages,
		p;
	for ( p in pages ) {
		pages[ p ].categories.forEach( function ( cat ) {
			console.log( cat.title );
		} );
	}
} );

Mögliche Fehler

Code Information
clshow Incorrect parameter - mutually exclusive values may not be supplied.

Parametergeschichte

  • v1.20: Eingeführt cldir
  • v1.16: Eingeführt clprop=hidden
  • v1.15: Eingeführt clcategories
  • v1.14: Eingeführt clshow
  • v1.13: Eingeführt clcontinue, cllimit, clprop=timestamp

Siehe auch

  • API:Kategorienmitglieder - listet Seiten auf, die Mitglieder einer bestimmten Kategorie sind.
  • API:Alle Seiten - listet alle Seiten auf, die ein bestimmtes Kriterium erfüllen; es kann auch auf den Kategorien-Namensraum zugreifen.
  • API:Alle Kategorien - ein list -Modul, das Kategorien aus dem gesamten Wiki erhält, basierend auf einem bestimmten Kriterium mit Bezug zum Titel der Kategorie.