API:分类
Appearance
本页是MediaWiki Action API帮助文档的一部份。 |
MediaWiki版本: | ≥ 1.11 |
GET 请求查看与一个或多个页面关联的分类 。
该模块可用作生成器 。
API帮助文档
示例
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"
},
...
]
}
}
}
}
示例代码
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 );
} );
}
} );
可能的错误
代码 | 信息 |
---|---|
clshow | 不正确的参数——不可提供互斥值。 |
参数历史
- v1.20: 启用
cldir
- v1.16: 启用
clprop=hidden
- v1.15: 启用
clcategories
- v1.14: 启用
clshow
- v1.13: 启用
clcontinue
,cllimit
,clprop=timestamp
参见
- API:Categorymembers - 列出属于某个分类的页面。
- API:所有页面 - 列出符合特定条件的所有页面;它还可以访问命令空间 分类 。
- API:所有分类 - 一个
list
模块,根据与分类标题相关的特定标准,从整个wiki中获取分类。