通过 pageId 获取维基百科页面 url

Get wikipedia page url by pageId

我使用 wikipedia api 从随机页面中获取摘录。

有范例回应:

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

现在我知道 pageid 这个页面了。

如何通过pageid获得此页面的url

您可以通过添加 info property and inprop=url. It can be combined with the extracts property 从 API 请求它来获得 URL,如下所示:prop=info|extracts&inprop=url.

例如:

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

您也可以只在 URL 本身中使用 pageidhttps://en.wikipedia.org/?curid=864588