API:重定向
Appearance
本页是MediaWiki Action API帮助文档的一部份。 |
MediaWiki版本: | ≥ 1.24 |
GET request to return all redirects to the given page(s).
API帮助文档
示例
GET请求
回应
{
"batchcomplete": "",
"query": {
"pages": {
"571857": {
"pageid": 571857,
"ns": 0,
"title": "Jacques Kallis",
"redirects": [
{
"pageid": 4388536,
"ns": 0,
"title": "JH Kallis"
},
{
"pageid": 10977683,
"ns": 0,
"title": "Kallis"
},
{
"pageid": 16779878,
"ns": 0,
"title": "Jack kallis"
}
...
]
}
}
}
}
示例代码
Python
#!/usr/bin/python3
"""
get_redirects.py
MediaWiki API Demos
Demo of `Redirects` module: Get all redirects to the given page(s)
MIT License
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
"action": "query",
"format": "json",
"titles": "Jacques Kallis",
"prop": "redirects"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
PAGES = DATA["query"]["pages"]
for k, v in PAGES.items():
for re in v["redirects"]:
print(re["title"] + " redirect to " + v["title"])
PHP
<?php
/*
get_redirects.php
MediaWiki API Demos
Demo of `Redirects` module: Get all redirects to the given page(s)
MIT License
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
"action" => "query",
"titles" => "Jacques Kallis",
"prop" => "redirects",
"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"]["pages"] as $page ) {
foreach( $page["redirects"] as $k => $v ) {
echo( $v["title"] . " redirect to " . $page["title"] . "\n");
}
}
JavaScript
/*
get_redirects.js
MediaWiki API Demos
Demo of `Redirects` module: Get all redirects to the given page(s)
MIT License
*/
var url = "https://en.wikipedia.org/w/api.php";
var params = {
action: "query",
titles: "Jacques Kallis",
prop: "redirects",
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 p in pages) {
for (var re of pages[p].redirects) {
console.log(re.title + " redirect to " + pages[p].title );
}
}
})
.catch(function(error){console.log(error);});
MediaWiki JS
/*
get_redirects.js
MediaWiki API Demos
Demo of `Redirects` module: Get all redirects to the given page(s)
MIT License
*/
var params = {
action: 'query',
titles: 'Jacques Kallis',
prop: 'redirects',
format: 'json'
},
api = new mw.Api();
api.get( params ).done( function ( data ) {
var pages = data.query.pages,
p;
function result( p ) {
pages[ p ].redirects.forEach( function ( re ) {
console.log( re.title + ' redirect to ' + pages[ p ].title );
} );
}
for ( p in pages ) {
result( p );
}
} );
可能的错误
代码 | 信息 |
---|---|
show | 不正确的参数——不可提供互斥值。 |
附加提醒
- 该模块可用作生成器 。
- Double redirects are not returned (e.g., a redirect page that links to another redirect page).