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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With