Jump to content

API:Iwlinks

From mediawiki.org
This page is a translated version of the page API:Iwlinks and the translation is 100% complete.
MediaWiki版本:
1.17

请求列出给定页面的跨维基链接。

API帮助文档

(main | query | iwlinks)

Returns all interwiki links from the given pages.

Specific parameters:
Other general parameters are available.
iwprop

Which additional properties to get for each interwiki link:

url
Adds the full URL.
Values (separate with | or alternative): url
iwprefix

Only return interwiki links with this prefix.

iwtitle

Interwiki link to search for. Must be used with iwprefix.

iwdir

The direction in which to list.

One of the following values: ascending, descending
Default: ascending
iwlimit

How many interwiki links to return.

Type: integer or max
The value must be between 1 and 500.
Default: 10
iwcontinue

When more results are available, use this to continue. More detailed information on how to continue queries can be found on mediawiki.org.

iwurl
Deprecated.

Whether to get the full URL (cannot be used with iwprop).

Type: boolean (details)


示例

GET请求

Albert Einstein获取跨维基链接列表。

回应

{
    "query": {
        "pages": [
            {
                "pageid": 736,
                "ns": 0,
                "title": "Albert Einstein",
                "iwlinks": [
                    {
                        "prefix": "b",
                        "title": "Introduction_to_Astrophysics/Albert_Einstein"
                    },
                    {
                        "prefix": "c",
                        "title": "Special:Search/Albert_Einstein"
                    },
                    {
                        "prefix": "commons",
                        "title": "Special:Search/Albert_Einstein"
                    },
                    ...                   
                ]
            }
        ]
    }
}

示例代码

Python

#!/usr/bin/python3

"""
    python/get_iwlinks.py

    MediaWiki API Demos
    Demo of `Iwlinks` module: Get the interwiki links from a given page.

    MIT License
"""

import requests

S = requests.Session()

URL = "https://en.wikipedia.org/w/api.php"

PARAMS = {
    "action": "query",
    "format": "json",
    "prop": "iwlinks",
    "titles": "Albert Einstein"
}

R = S.get(url=URL, params=PARAMS)
DATA = R.json()

PAGES = DATA["query"]["pages"]

for k, v in PAGES.items():
    print(v["iwlinks"])

PHP

<?php
/*
    get_iwlinks.php

    MediaWiki API Demos
    Demo of `Iwlinks` module: Get the interwiki links from a given page.

    MIT License
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "format" => "json",
    "prop" => "iwlinks",
    "titles" => "Albert Einstein"
];

$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 ) {
    var_dump( $v["iwlinks"] );
}

JavaScript

/*
    get_iwlinks.js

    MediaWiki API Demos
    Demo of `Iwlinks` module: Get the interwiki links from a given page.

    MIT License
*/

var url = "https://en.wikipedia.org/w/api.php"; 

var params = {
    action: "query",
    format: "json",
    prop: "iwlinks",
    titles: "Albert Einstein"
};

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) {
            console.log(pages[p].iwlinks);
        }
    })
    .catch(function(error){console.log(error);});

MediaWiki JS

/*
	get_iwlinks.js

	MediaWiki API Demos
	Demo of `Iwlinks` module: Get the interwiki links from a given page.

	MIT License
*/

var params = {
		action: 'query',
		format: 'json',
		prop: 'iwlinks',
		titles: 'Albert Einstein'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	var pages = data.query.pages,
		p;
	for ( p in pages ) {
		console.log( pages[ p ].iwlinks );
	}
} );

可能的错误

代码 信息
invalidparammix title参数只能与prefix一起使用。

参数历史

  • v1.24:
    • 启用iwprop, url
    • 弃用iwurl
  • v1.19: 启用iwdir
  • v1.18: 启用iwprefix, iwtitle

參閱