python-2.7wikipedia-apiwikidatawikidata-api

How to get Wikipedia page from Wikidata Id?


How to get Wikipedia page (in a particular language, say French) from the Wikidata Id (ex: Q19675)? The question seems obvious but strangely, I find nothing on the web. I'm looking for a url command that I could use with requests Python module, something like:

url = "https://www.wikidata.org/w/api.php?action=some_method&ids=Q19675"
r = requests.post(url, headers={"User-Agent" : "Magic Browser"})

Someone can help me?


Solution

  • You have to use MediaWiki API with action=wbgetentities:

    https://www.wikidata.org/w/api.php?action=wbgetentities&format=xml&props=sitelinks&ids=Q19675&sitefilter=frwiki
    

    where:

    For your example response will be:

    <api success="1">
        <entities>
            <entity type="item" id="Q19675">
                <sitelinks>
                    <sitelink site="frwiki" title="Musée du Louvre">
                        <badges/>
                    </sitelink>
                </sitelinks>
            </entity>
        </entities>
    </api>