This page is a translated version of the page API:Links and the translation is 100% complete.
เวอร์ชันมีเดียวิกิ:
1.11

GET request เพื่อค้นหาลิงก์ทั้งหมดบนหน้าที่ให้ไว้

โมดูลนี้สามารถใช้เป็น generator ได้

API documentation


(main | query | links)
  • This module requires read rights.
  • This module can be used as a generator.
  • Source: MediaWiki
  • License: GPL-2.0-or-later

Returns all links from the given pages.

Specific parameters:
Other general parameters are available.
plnamespace

Show links in these namespaces only.

Values (separate with | or alternative): -1, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 90, 91, 92, 93, 100, 101, 102, 103, 104, 105, 106, 107, 486, 487, 710, 711, 828, 829, 1198, 1199, 2600, 5500, 5501
To specify all values, use *.
pllimit

How many links to return.

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

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

pltitles

Only list links to these titles. Useful for checking whether a certain page links to a certain title.

Separate values with | or alternative.
Maximum number of values is 50 (500 for clients that are allowed higher limits).
pldir

The direction in which to list.

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

ตัวอย่าง

ตัวอย่างที่ 1: ดึงลิงก์ทั้งหมดในหน้า

GET request

รับรายการลิงก์จากหน้าวิกิพีเดียภาษาอังกฤษที่ 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"
                    },
                    ...
                ]
            }
        ]
    }
}

รหัสตัวอย่าง

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 );
		} );
	}
} );

ตัวอย่างที่ 2: ดึงลิงก์ที่ขาดหายไป

รับคำขอให้ดึงลิงก์ที่ขาดหายไปหรือสีแดงใน Wikipedia:Most-wanted_articles สองขั้นตอนในการทำเช่นนั้น:

  • ทำการร้องขอ GET ไปยัง Action API เพื่อส่งคืนลิงก์ทั้งหมดที่ฝังอยู่ในหน้าที่ให้มา
  • จากการสกัดเพิ่มเติม รับลิงก์ที่ขาดหายไปและยังไม่มีอยู่ในวิกิพีเดียภาษาอังกฤษ
โมดูลย่อยของโมดูลการสืบค้น generator=links links ใช้เป็นโมดูล generator เพื่อรับชุดลิงก์ที่ฝังอยู่ในหน้า

GET request


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"
      }
      ...
    }
  }
}

รหัสตัวอย่าง

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: เปิดตัว pldir
  • v1.17: เปิดตัว pltitles
  • v1.13: เปิดตัว pllimit, plcontinue

ดูเพิ่ม

  • API:Linkshere - โมดูลย่อย API:Properties ที่ค้นหาหน้าที่มีลิงก์ไปยังหน้าที่ระบุ
  • API:Backlinks - โมดูลย่อย API:Lists ที่ได้รับรายการของหน้าที่เชื่อมโยงไปยังหน้าที่กำหนด
  • API:Iwlinks - ค้นหาลิงก์วิกิข้ามภาษาในหน้าที่กำหนด (เช่น หน้าเมทา หน้าพิเศษ)
  • API:Iwbacklinks - รับรายการหน้าที่เชื่อมโยงจากลิงก์ระหว่างวิกิที่กำหนด
  • API:Extlinks - ค้นหาลิงก์ภายนอกทั้งหมดในหน้าที่กำหนด
  • API:Exturlusage - รับหน้าทั้งหมดที่เชื่อมโยงไปยัง URL ภายนอก
  • API:Langlinks - รับรายการลิงก์ภาษาจากหน้าที่กำหนด ลิงก์ภาษาแสดงถึงการแปล
  • API:Langbacklinks - รับรายการหน้าที่มีลิงก์ภาษาที่ระบุ