|  | This page is part of the MediaWiki Action API documentation. | 
| MediaWiki version: | ≥ 1.11 | 
GET request to find all the links on the provided page(s).
This module can be used as a {{ll|API:Query#Generators|generator}}.
API documentation
| 
 Special:ApiHelp/query+links | 
Examples
Example 1: Fetch all the links in a page
GET request
Get a list of links from the English Wikipedia's page on w:Albert Einstein
Response
{
    "query": {
        "pages": [
            {
                "pageid": 736,
                "ns": 0,
                "title": "Albert Einstein",
                "links": [
                    {
                        "ns": 0,
                        "title": "2dF Galaxy Redshift Survey"
                    },
                    {
                        "ns": 0,
                        "title": "A priori and a posteriori"
                    },
                    {
                        "ns": 0,
                        "title": "Aage Bohr"
                    },
                    ...
                ]
            }
        ]
    }
}
Sample code
Python
#!/usr/bin/python3
"""
    get_links.py
    MediaWiki API Demos
    Demo of `Links` module: Get all links on 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": "Albert Einstein",
    "prop": "links"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
PAGES = DATA["query"]["pages"]
for k, v in PAGES.items():
    for l in v["links"]:
        print(l["title"])
PHP
<?php
/*
    get_links.php
    MediaWiki API Demos
    Demo of `Links` module: Get all links on the given page(s)
    MIT License
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "format" => "json",
    "titles" => "Albert Einstein",
    "prop" => "links"
];
$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["links"] as $k => $v ) {
        echo( $v["title"] . "\n" );
    }
}
JavaScript
/*
    get_links.js
    MediaWiki API Demos
    Demo of `Links` module: Get all links on the given page(s)
    MIT License
*/
var url = "https://en.wikipedia.org/w/api.php"; 
var params = {
    action: "query",
    format: "json",
    titles: "Albert Einstein",
    prop: "links"
};
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 l of pages[p].links) {
                console.log(l.title);
            }
        }
    })
    .catch(function(error){console.log(error);});
MediaWiki JS
/*
	get_links.js
	MediaWiki API Demos
	Demo of `Links` module: Get all links on the given page(s)
	MIT License
*/
var params = {
		action: 'query',
		format: 'json',
		titles: 'Albert Einstein',
		prop: 'links'
	},
	api = new mw.Api();
api.get( params ).done( function ( data ) {
	var pages = data.query.pages,
		p;
	for ( p in pages ) {
		pages[ p ].links.forEach( function ( l ) {
			console.log( l.title );
		} );
	}
} );
Example 2: Fetch missing links
Get request to fetch missing or red links on Wikipedia:Most-wanted_articles. Two steps to do so:
- Make a GETrequest to the Action API to return all links embedded on the provided page.
- From further extraction, obtain those links that are missing and don't yet exist on English Wikipedia.
generator=links query module's submodule links used as a generator module to get a set of links embedded on a page.GET request
api.php?action=query&format=json&generator=links&titles=Wikipedia:Most-wanted_articles&gpllimit=20      [try in ApiSandbox]
Response
| Response | 
|---|
| {
  "batchcomplete": "", 
  "continue": {
    "continue": "gplcontinue||", 
    "gplcontinue": "297177|0|1965_in_sumo"
  }, 
  "query": {
    "pages": {
      "-1": {
        "missing": "", 
        "ns": 0, 
        "title": "(viii)"
      }, 
      "-10": {
        "missing": "", 
        "ns": 0, 
        "title": "1954 in sumo"
      }
      ...
    }
  }
}
 | 
Sample code
| get_red_links.py | 
|---|
| Python#!/usr/bin/python3
"""
    get_red_links.py
    MediaWiki API Demos
    Demo of `Links` module to identify red or missing links on a page.
    MIT License
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
    "action": "query",
    "titles": "Wikipedia:Most-wanted_articles",
    "gpllimit": "20",
    "format": "json",
    "generator": "links"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
PAGES = DATA['query']['pages']
for page in PAGES.values():
    if 'missing' in page:
        print(page['title'])
PHP<?php
/*
    get_red_links.php
    MediaWiki API Demos
    Demo of `Links` module to identify red or missing links on a page.
    MIT License
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "generator" => "links",
    "titles" => "Wikipedia:Most-wanted_articles",
    "gpllimit" => "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"]["pages"] as $page ){
    if( array_key_exists("missing",$page ) ){
        echo( $page["title"] . "\n" );
    }
}
JavaScript/*
    get_red_links.js
    MediaWiki API Demos
    Demo of `Links` module to identify red or missing links on a page.
    MIT License
*/
var url = "https://en.wikipedia.org/w/api.php"; 
var params = {
    action: "query",
    generator: "links",
    titles: "Wikipedia:Most-wanted_articles",
    gpllimit: "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.pages;
        for (var p in pages) {
            if(pages[p].hasOwnProperty("missing")){
                console.log(pages[p].title);
            }
        }
    })
    .catch(function(error){console.log(error);});
MediaWiki JS/*
	get_red_links.js
	MediaWiki API Demos
	Demo of `Links` module to identify red or missing links on a page.
	MIT License
*/
var params = {
		action: 'query',
		generator: 'links',
		titles: 'Wikipedia:Most-wanted_articles',
		gpllimit: '20',
		format: 'json'
	},
	api = new mw.Api();
api.get( params ).done( function ( data ) {
	var pages = data.query.pages,
		p;
	for ( p in pages ) {
		if( pages[ p ].hasOwnProperty('missing') ){
			console.log( pages[ p ].title );
		}
	}
} );
 | 
Parameter history
- v1.19:  Introduced pldir
- v1.17:  Introduced pltitles
- v1.13:  Introduced pllimit,plcontinue
See also
- API:Linkshere - API:Properties submodule that finds pages which include a link to the given page.
- API:Backlinks - API:Lists submodule that gets a list of pages which link to a given page.
- API:Iwlinks - Find interwiki links on a given page (i.e, meta pages, special pages).
- API:Iwbacklinks - Get a list of pages which are linked from a given interwiki link.
- API:Extlinks - Find all external links on a given page.
- API:Exturlusage - Get all pages that link to an external URL.
- API:Langlinks - Get a list of language links from the given page. Language links represent translations.
- API:Langbacklinks - Get a list of pages that contains a given language link.
    This article is issued from Mediawiki. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.
