API:Categorymembers
Appearance
Ta strona jest częścią dokumentacji API akcji MediaWiki. |
Wersja MediaWiki: | ≥ 1.11 |
Żądanie GET zwracające listę stron należących do danej kategorii.
Dokumentacja API
Przykład 1: Wymień dwanaście stron w kategorii
Żądanie GET
api.php? action=query& list=categorymembers& cmtitle=Category:Physics& cmlimit=20 [wypróbuj w ApiSandbox]
Odpowiedź
Odpowiedź |
---|
{
"api": {
"query-continue": {
"categorymembers": {
"_cmcontinue": "subcat|44594e414d494353|10998823"
}
},
"query": {
"categorymembers": {
"cm": [
{
"_pageid": "22688097",
"_ns": "0",
"_title": "Branches of physics"
},
{
"_pageid": "3445246",
"_ns": "0",
"_title": "Glossary of classical physics"
},
{
"_pageid": "24489",
"_ns": "0",
"_title": "Outline of physics"
},
...
]
}
}
}
}
|
Przykładowy kod
get_category_items.py |
---|
Python#!/usr/bin/python3
"""
get_category_items.py
MediaWiki API Demos
Demo of `Categorymembers` module : List twenty items in a category
MIT License
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
"action": "query",
"cmtitle": "Category:Physics",
"cmlimit": "20",
"list": "categorymembers",
"format": "json"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
PAGES = DATA['query']['categorymembers']
for page in PAGES:
print(page['title'])
PHP<?php
/*
get_category_items.php
MediaWiki API Demos
Demo of `Categorymembers` module : List twenty items in a category
MIT License
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
"action" => "query",
"list" => "categorymembers",
"cmtitle" => "Category:Physics",
"cmlimit" => "20",
"format" => "json"
];
$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"]["categorymembers"] as $pages ){
echo( $pages["title"] . "\n" );
}
JavaScript/*
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",
list: "categorymembers",
cmtitle: "Category:Physics",
cmlimit: "20",
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.categorymembers;
for (var page in pages) {
console.log(pages[page].title);
}
})
.catch(function(error){console.log(error);});
MediaWiki JS/*
get_category_items.js
MediaWiki API Demos
Demo of `Categorymembers` module : List twenty items in a category
MIT License
*/
var params = {
action: 'query',
list: 'categorymembers',
cmtitle: 'Category:Physics',
cmlimit: '20',
format: 'json'
},
api = new mw.Api();
api.get( params ).done( function ( data ) {
var pages = data.query.categorymembers,
page;
for ( page in pages ) {
console.log( pages[ page ].title );
}
} );
|
Przykład 2: Pozyskaj dziesięć artykułów ostatnio dodanych do kategorii
Żądanie GET
api.php? action=query& list=categorymembers& cmtitle=Category:Physics& cmsort=timestamp& cmdir=desc [wypróbuj w ApiSandbox]
Odpowiedź
Odpowiedź |
---|
{
"api": {
"query-continue": {
"categorymembers": {
"_cmcontinue": "Magnetic levitation|"
}
},
"query": {
"categorymembers": {
"cm": [
{
"_pageid": "1653925",
"_ns": "100",
"_title": "Portal:Physics"
},
{
"_pageid": "22939",
"_ns": "0",
"_title": "Physics"
},
{
"_pageid": "3445246",
"_ns": "0",
"_title": "Glossary of classical physics"
},
...
]
}
}
}
}
|
Przykładowy kod
get_recent_category_items.py |
---|
Python#!/usr/bin/python3
"""
get_recent_category_items.py
MediaWiki API Demos
Demo of `Categorymembers` module : Get the ten articles most recently added to a category
MIT License
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
"cmdir": "desc",
"format": "json",
"list": "categorymembers",
"action": "query",
"cmtitle": "Category:Physics",
"cmsort": "timestamp"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
PAGES = DATA["query"]["categorymembers"]
for page in PAGES:
print(page["title"])
PHP<?php
/*
get_recent_category_items.php
MediaWiki API Demos
Demo of `Categorymembers` module : Get the ten articles most recently added to a category
MIT License
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
"action" => "query",
"list" => "categorymembers",
"cmtitle" => "Category:Physics",
"cmsort" => "timestamp",
"cmdir" => "desc",
"format" => "json"
];
$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"]["categorymembers"] as $page ) {
echo( $page["title"] . "\n" );
}
JavaScript/*
get_recent_category_items.js
MediaWiki API Demos
Demo of `Categorymembers` module : Get the ten articles most recently added to a category
MIT License
*/
var url = "https://en.wikipedia.org/w/api.php";
var params = {
action: "query",
list: "categorymembers",
cmtitle: "Category:Physics",
cmsort: "timestamp",
cmdir: "desc",
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.categorymembers;
for (var page in pages) {
console.log(pages[page].title);
}
})
.catch(function(error){console.log(error);});
MediaWiki JS/*
get_recent_category_items.js
MediaWiki API Demos
Demo of `Categorymembers` module : Get the ten articles most recently added to a category
MIT License
*/
var params = {
action: 'query',
list: 'categorymembers',
cmtitle: 'Category:Physics',
cmsort: 'timestamp',
cmdir: 'desc',
format: 'json'
},
api = new mw.Api();
api.get( params ).done( function ( data ) {
var pages = data.query.categorymembers,
page;
for ( page in pages ) {
console.log( pages[ page ].title );
}
} );
|
Przykład 3: Pozyskaj listę dziesięciu podkategorii danej kategorii
Żądanie GET
api.php? action=query& list=categorymembers& cmtitle=Category:Wikipedia& cmtype=subcat [wypróbuj w ApiSandbox]
Odpowiedź
Odpowiedź |
---|
{
"api": {
"query-continue": {
"categorymembers": {
"_cmcontinue": "subcat|57494b4950454449412050454f504c45|41491664"
}
},
"query": {
"categorymembers": {
"cm": [
{
"_pageid": "1458692",
"_ns": "14",
"_title": "Category:Wikipedias by language"
},
{
"_pageid": "22918730",
"_ns": "14",
"_title": "Category:Books about Wikipedia"
},
{
"_pageid": "16957584",
"_ns": "14",
"_title": "Category:Critics of Wikipedia"
},
...
]
}
}
}
}
|
Następne podkategorie (jeśli jest ich ponad 10) mogą być kontynuowane przy użyciu parametru cmcontinue
z odpowiedzi powyżej.
Przykładowy kod
get_subcategories.py |
---|
Python#!/usr/bin/python3
"""
get_subcategories.py
MediaWiki API Demos
Demo of `Categorymembers` module : Get ten subcategories of a category
MIT License
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
"action": "query",
"cmtitle": "Category:Wikipedia",
"cmtype": "subcat",
"list": "categorymembers",
"format": "json"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
PAGES = DATA["query"]["categorymembers"]
for page in PAGES:
print(page["title"])
PHP<?php
/*
get_subcategories.php
MediaWiki API Demos
Demo of `Categorymembers` module : Get ten subcategories of a category
MIT License
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
"action" => "query",
"list" => "categorymembers",
"cmtitle" => "Category:Wikipedia",
"cmtype" => "subcat",
"format" => "json"
];
$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"]["categorymembers"] as $cat ) {
echo( $cat["title"] . "\n");
}
JavaScript/*
get_subcategories.js
MediaWiki API Demos
Demo of `Categorymembers` module : Get ten subcategories of a category
MIT License
*/
var url = "https://en.wikipedia.org/w/api.php";
var params = {
action: "query",
list: "categorymembers",
cmtitle: "Category:Wikipedia",
cmtype: "subcat",
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 category = response.query.categorymembers;
for (var cat in category) {
console.log(category[cat].title);
}
})
.catch(function(error){console.log(error);});
MediaWiki JS/*
get_subcategories.js
MediaWiki API Demos
Demo of `Categorymembers` module : Get ten subcategories of a category
MIT License
*/
var params = {
action: 'query',
list: 'categorymembers',
cmtitle: 'Category:Wikipedia',
cmtype: 'subcat',
format: 'json'
},
api = new mw.Api();
api.get( params ).done( function ( data ) {
var category = data.query.categorymembers,
cat;
for ( cat in category ) {
console.log( category[ cat ].title );
}
} );
|
Możliwe błędy
Kod | Info |
---|---|
cmnotitle | The parameter cmtitle is required. |
cminvalidcategory | Wprowadzona nazwa kategorii jest nieprawidłowa. |
cmbadcontinue | Invalid continue param. You should pass the original value returned by the previous query. |
Historia parametrów
- v1.24: Przestarzałe:
cmstartsortkey
,cmendsortkey
- v1.18: Wprowadzone:
cmstartsortkeyprefix
,cmendsortkeyprefix
- v1.17: Wprowadzone:
sortkeyprefix
,type
- v1.14: Wprowadzone:
cmstartsortkey
,cmendsortkey
- v1.12: Wprowadzone:
cmtype
,cmstart
,cmend
,cmdir
Zobacz też
- API:Allpages - listy wszystkich stron spełniających podane wymagania
- API:Kategorie - listy kategorii związanych ze stroną
- API:Allcategories - listy wszystkich kategorii na wiki