This page is a translated version of the page API:Categoryinfo and the translation is 100% complete.
MediaWiki版本:
1.13

GET 请求查看有关用类别的信息。

API帮助文档


prop=categoryinfo (ci)

(main | query | categoryinfo)

Returns information about the given categories.

Specific parameter:
Other general parameters are available.
cicontinue

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

Example:
Get information about Category:Foo and Category:Bar.
api.php?action=query&prop=categoryinfo&titles=Category:Foo|Category:Bar [open in sandbox]

示例

GET请求

获取有关一些类别的信息


回应

{
    "batchcomplete": "",
    "query": {
        "pages": {
            "-1": {
                "ns": 14,
                "title": "Category:Foo",
                "missing": "",
                "categoryinfo": {
                    "size": 1,
                    "pages": 1,
                    "files": 0,
                    "subcats": 0
                }
            },
            "1504": {
                "pageid": 1504,
                "ns": 0,
                "title": "Albert"
            },
            "3108204": {
                "pageid": 3108204,
                "ns": 14,
                "title": "Category:Infobox templates",
                "categoryinfo": {
                    "size": 2066,
                    "pages": 2048,
                    "files": 0,
                    "subcats": 18
                }
            }
        }
    }
}

示例代码

Python

#!/usr/bin/python3

"""
    get_category_info.py

    MediaWiki API Demos
    Demo of `Categoryinfo` module: Get information about few categories
    MIT license
"""

import requests

S = requests.Session()

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

PARAMS = {
    "action": "query",
    "format": "json",
    "titles": "Category:Foo|Category:Infobox templates",
    "prop": "categoryinfo"
}

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

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

for k, v in PAGES.items():
    print(v["title"] + " has " + str(v["categoryinfo"]["pages"]) + " pages.")

PHP

<?php

//This file is autogenerated. See modules.json and autogenerator.py for details

/*
    get_category_info.php

    MediaWiki API Demos
    Demo of `Categoryinfo` module: Get information about a few categories

    MIT License
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "format" => "json",
    "titles" => "Category:Foo|Category:Infobox templates",
    "prop" => "categoryinfo"
];

$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 ) {
    echo( $v["title"] . " has " . $v["categoryinfo"]["pages"] . " pages." . "\n"  );
}

JavaScript

//This file is autogenerated. See modules.json and autogenerator.py for details

/*
    get_category_items.js

    MediaWiki API Demos
    Demo of `Categorymembers` module : List twenty items in a category

    MIT License
*/

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

var params = {
    action: "query",
    prop: "categoryinfo",
    titles: "Category:Foo|Category:Infobox templates",
    format: "json"
};

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 page in pages) {
            console.log(pages[page].title + " has " + pages[page].categoryinfo.pages + " pages." );
        }
    })
    .catch(function(error){console.log(error);});

MediaWiki JS

// This file is autogenerated. See modules.json and autogenerator.py for details

/*
	get_category_info.js

	MediaWiki API Demos
	Demo of `Categoryinfo` module: Get information about a few categories

	MIT License
*/

var params = {
		action: 'query',
		format: 'json',
		titles: 'Category:Foo|Category:Infobox templates',
		prop: 'categoryinfo'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	var pages = data.query.pages,
		page;
	for ( page in pages ) {
		console.log( pages[ page ].title + ' has ' + pages[ page ].categoryinfo.pages + ' pages.' );
	}
} );

其他说明

  • 如果分类页面存在,但没有成员,则API响应中将包含categoryinfo字段“不”。
  • 如果有更多可用结果,请使用cicontinue参数继续。

參見