API:所有分类
本页是MediaWiki Action API帮助文档的一部份。 |
MediaWiki版本: | ≥ 1.12 |
GET 请求列出所有符合其标题的特定条件的分类页 。
该模块可用作生成器 。
API帮助文档
示例
GET请求
从“15th-century caliphs”开始,获取所有分类的列表。
响应
{
{
"batchcomplete": "",
"continue": {
"accontinue": "15th-century_churches_in_Denmark",
"continue": "-||"
},
"query": {
"allcategories": [
{
"*": "15th-century caliphs"
},
{
"*": "15th-century calligraphers"
},
{
"*": "15th-century card games"
},
...
]
}
}
示例代码
Python
#!/usr/bin/python3
"""
get_allcategories.py
MediaWiki API Demos
Demo of `Allcategories` module: Get all categories, starting from a
certain point, as ordered by category title.
MIT License
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
"action": "query",
"format": "json",
"list": "allcategories",
"acfrom": "15th-century caliphs"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
CATEGORIES = DATA["query"]["allcategories"]
for cat in CATEGORIES:
print(cat["*"])
PHP
<?php
/*
get_allcategories.php
MediaWiki API Demos
Demo of `Allcategories` module: Get all categories, starting from a certian point, as ordered by category title.
MIT License
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
"action" => "query",
"format" => "json",
"list" => "allcategories",
"acfrom" => "15th-century caliphs"
];
$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"]["allcategories"] as $k => $v ) {
echo( $v["*"] . "\n" );
}
JavaScript
/*
get_allcategories.js
MediaWiki API Demos
Demo of `Allcategories` module: Get all categories, starting from a certain point, as ordered by category title.
MIT License
*/
var url = "https://en.wikipedia.org/w/api.php";
var params = {
action: "query",
format: "json",
list: "allcategories",
acfrom: "15th-century caliphs"
};
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 categories = response.query.allcategories;
for (var cat in categories) {
console.log(categories[cat]["*"]);
}
})
.catch(function(error){console.log(error);});
MediaWiki JS
/*
get_allcategories.js
MediaWiki API Demos
Demo of `Allcategories` module: Get all categories,
starting from a certian point, as ordered by category title.
MIT License
*/
var params = {
action: 'query',
format: 'json',
list: 'allcategories',
acfrom: '15th-century caliphs'
},
api = new mw.Api();
api.get( params ).done( function ( data ) {
var categories = data.query.allcategories,
cat;
for ( cat in categories ) {
console.log( categories[ cat ][ '*' ] );
}
} );
附加提醒
- 此模块与list=allpages&alnamespace=14 的不同之处在于,将列出没有描述的类别,而不会列出从未使用过该类别的重定向和页面。
- 返回内容可能包括以前使用但后来被删除的分类。
- 由于返回内容可能包含已删除或以其他方式清空的分类,因此建议使用
acmin=1
过滤列表,以便仅返回包含一个或多个页面的分类。
參閱
- API:Categorymembers - 列出属于某个分类的页面。
- API:Categories -
prop
模块,可以获取与特定页面关联的所有分类。 - API:所有页面 - 另一个可以访问分类名字空间 的模块。