Alexandr
Alexandr

Reputation: 3969

Get wikipedia page url by pageId

I use wikipedia api to get an extract from a random page.

There is example response:

{
    "batchcomplete": "",
    "continue": {
        "grncontinue": "0.701350797294|0.701351244349|4312122|0",
        "continue": "grncontinue||"
    },
    "query": {
        "pages": {
            "1485573": {
                "pageid": 1485573,
                "ns": 0,
                "title": "some title",
                "extract": "some text"
            }
        }
    }
}

And now I know pageid of this page.

How can I get url of this page by pageid?

Upvotes: 2

Views: 6048

Answers (1)

slaporte
slaporte

Reputation: 703

You can the get the URL by requesting it from the API by adding info property and inprop=url. It can be combined with the extracts property like this: prop=info|extracts&inprop=url.

For example:

https://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info|extracts&inprop=url

{
"continue": {
    "excontinue": 1,
    "continue": "||info"
},
"query": {
    "pages": {
        "864588": {
            "pageid": 864588,
            "ns": 0,
            "title": "Benbane Head",
            ...
            "fullurl": "https://en.wikipedia.org/wiki/Benbane_Head",
            "editurl": "https://en.wikipedia.org/w/index.php?title=Benbane_Head&action=edit",
            "canonicalurl": "https://en.wikipedia.org/wiki/Benbane_Head",
            "extract": "<p><span></span></p>\n\n<p><b>Benbane Head</b>, or <b>Benbane</b> (from Irish <i>an Bhinn Bh\u00e1n</i>, meaning \"the white headland\"), is the northernmost point of mainland Northern Ireland. It is in County Antrim, near the Giant's Causeway, which lies between Causeway Head and Benbane Head. The nearest settlements are Bushmills and Portballintrae.</p>\n<h2><span id=\"References\">References</span></h2>\n\n<p><br></p>"
        }
    }
}

You can also just use the pageid in the URL itself: https://en.wikipedia.org/?curid=864588

Upvotes: 11

Related Questions