Yue Cao
Yue Cao

Reputation: 71

How to get Wikipedia page id 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.

Upvotes: 7

Views: 5033

Answers (3)

Stanislav Kralin
Stanislav Kralin

Reputation: 11479

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.

Upvotes: 5

Irshad Khan
Irshad Khan

Reputation: 6046

Use below URL in your CURL call. You have to change WikiDataID Q243 in below link.

For Example if you want wikiPageID of Taj_Mahal then replace Q243 with Q9141 in below link and do CURL call.

http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=PREFIX+wd%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E+%0D%0ASELECT+%3FwikiPageID+WHERE+%7B%0D%0A%3Fdbpedia_id+owl%3AsameAs+%3Fwikidata_id++.%0D%0A%3Fdbpedia_id+dbo%3AwikiPageID+%3FwikiPageID+.%0D%0AVALUES+%28%3Fwikidata_id%29+%7B%28wd%3AQ243%29%7D+%0D%0A%7D&format=application%2Fsparql-results%2Bjson&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=30000&debug=on&run=+Run+Query

To Get WikiPageID through wikiDataId you have to modify above link or by replacing wikiDataID of your choice in above link.

Note:

1) To Get WikiPageID with Label use this URL in CURL Call

2) Find Q243 and Replace with your wikiDataID

Upvotes: 1

maxlath
maxlath

Reputation: 1841

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

Upvotes: 4

Related Questions