Jump to content

User:Martyav/Sandbox/API:Links

From mediawiki.org
MediaWiki version:
1.11

GET Request to find all the links on the provided page(s).

This module can be used as a generator.

API documentation

[edit]
(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


Example

[edit]

GET Request

[edit]
Get a list of links from the English Wikipedia's page on en:Albert Einstein

Reponse

[edit]
{
    "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"
                    },
                    {
                        "ns": 0,
                        "title": "Aarau"
                    },
                    {
                        "ns": 0,
                        "title": "Aargau"
                    },
                    ...
                ]
            }
        ]
    }
}

Sample code

[edit]

get_links.py

#!/usr/bin/python3

"""
    get_links.py

    MediaWiki Action API Code Samples
    Demo of `Links` module: Get all links 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": "Albert Einstein",
    "prop": "links"
}

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

print(DATA)

Parameter history

[edit]
  • v1.19: Introduced pldir
  • v1.17: Introduced pltitles
  • v1.13: Introduced pllimit, plcontinue

See also

[edit]