API:カテゴリ情報
このページは MediaWiki 操作 API の説明文書の一部です。 |
MediaWiki バージョン: | ≧ 1.13 |
GET request to obtain information about few categories.
APIの説明文書
例
GET リクエスト
Get info about a few categories
api.php? action=query& titles=Albert|Category:Foo|Category:Infobox%20templates& prop=categoryinfo [ApiSandbox で試用する]
レスポンス
{
"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.' );
}
} );
追加的な注記
- If the category page exists, but the category has no members, a
categoryinfo
field will not be included in the API response. - さらに結果がある場合は、
cicontinue
パラメーターを使って使用してください。