How to get Wikipedia page id from Wikidata id?

python, wikidata, wikidata-query-service, wikipedia

Solution

I'm not sure, if DBpedia always contains both wikiPageID and Wikidata ID, but you can try the folowing query on DBpedia:

PREFIX wd: <http://www.wikidata.org/entity/> 
SELECT ?wikipedia_id WHERE {
    ?dbpedia_id owl:sameAs ?wikidata_id  .
    ?dbpedia_id dbo:wikiPageID ?wikipedia_id .
    VALUES (?wikidata_id) {(wd:Q123)} 
}

Try it!

Or you can try the following federated query on Wikidata:

PREFIX wd: <http://www.wikidata.org/entity/> 
PREFIX owl: <http://www.w3.org/2002/07/owl#> 
PREFIX dbo: <http://dbpedia.org/ontology/>  

SELECT ?wikipedia_id where {
    VALUES (?wikidata_id)  {(wd:Q123)}
    SERVICE <http://dbpedia.org/sparql> {
       ?dbpedia_id owl:sameAs ?wikidata_id .
       ?dbpedia_id dbo:wikiPageID ?wikipedia_id 
    } 
}

Try it!

Update

You can call out to the Wikipedia API using MWAPI on Wikidata:

SELECT ?pageid WHERE {
    VALUES (?item) {(wd:Q123)} 
    [ schema:about ?item ; schema:name ?name ;
      schema:isPartOf <https://en.wikipedia.org/> ]
     SERVICE wikibase:mwapi {
         bd:serviceParam wikibase:endpoint "en.wikipedia.org" .
         bd:serviceParam wikibase:api "Generator" .
         bd:serviceParam mwapi:generator "allpages" .
         bd:serviceParam mwapi:gapfrom ?name .
         bd:serviceParam mwapi:gapto ?name .
         ?pageid wikibase:apiOutput "@pageid" .
    }
}

Try it!

Unfortunately, it seems you have to use a generator; `allpages` appears to be the most suitable one.

Problem

I want to get the Wikipedia page id from the Wikidata id, how can I get it from the Wikidata Query Service or other methods with python? Because I do not see any attribute in wikidata called something like wikipedia id.

Original source