Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Wikipedia pageid from Wikidata Id?

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.

like image 474
Yue Cao Avatar asked May 02 '17 20:05

Yue Cao


2 Answers

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.

like image 68
Stanislav Kralin Avatar answered Sep 19 '22 12:09

Stanislav Kralin


First, you need to get the Wikipedia page title from the Wikidata id, which can be done with a request to Wikidata API wbgetentities module, like so: https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q123&format=json&props=sitelinks

Then, once you found the Wikipedia title from the desired Wikipedia edition, you can get the associated page id from that Wikipedia API: https://en.wikipedia.org/w/api.php?action=query&titles=September&format=json

So from those example URLs you can get that:
Wikidata id = Q123
=> English Wikipedia (enwiki) title = September
=> pageid = 15580374

like image 39
maxlath Avatar answered Sep 17 '22 12:09

maxlath