Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Wikidata ID for an Wikipedia article by API?

How to obtain Wikidata ID from English Wikipedia article name, also for redirects? Is there API call?

Example:

Karachi >> Q8660
Karaachi >> Q8660

How to approach changes in assignment? Now Media in Karachi is redirect to Media of Pakistan (Wikidata item Q6805922), but can be own article later.

like image 438
Shoussi Elbril Avatar asked May 04 '16 10:05

Shoussi Elbril


People also ask

Does Wikidata have a API?

Display multilingual information from Wikidata in your application. This page shows how to retrieve and present relevant information from Wikidata by associating it with entities in your application.

How do I link Wikidata to Wikipedia?

Access from Wikipedia From a Wikipedia page, you can go to the link "Wikidata item", using "Tools" in the side panel (in the left), to see and edit it. Also in Tools, there is another link to "page information", where is "Wikidata item ID", that contains the QID (for example: Q171 or "None").

Does Wikipedia use Wikidata?

As of November 2018, Wikidata information was used in 58.4% of all English Wikipedia articles, mostly for external identifiers or coordinate locations.

How do I get data from Wikidata?

You can query the data in Wikidata through our SPARQL endpoint, the Wikidata Query Service. The service can be used both as an interactive web interface, or programmatically by submitting GET or POST requests to https://query.wikidata.org/sparql .


3 Answers

Use Wikipedia API to create a query with property pageprops and resolving redirects:

https://en.wikipedia.org/w/api.php?action=query&prop=pageprops&ppprop=wikibase_item&redirects=1&titles=ARTICLE_NAME

In your case, for Wikipedia articles "Media in Karachi" and "Media of Pakistan" you will get the same Wikidata item ID: Q6805922. Same thing for "Karaachi" and "Karachi" → Q8660.

like image 111
Termininja Avatar answered Sep 28 '22 13:09

Termininja


As well as the accepted answer you can also use the Wikidata API directly passing in a site and a title.

The docs are at https://www.wikidata.org/w/api.php?action=help&modules=wbgetentities

Get the entity for http://en.wikipedia.org/wiki/Karachi:

  • If you know the exact title: https://www.wikidata.org/w/api.php?action=wbgetentities&sites=enwiki&titles=Karachi
  • With title normalization, for initial character capitalization fixes, and underscores (and possible more): https://www.wikidata.org/w/api.php?action=wbgetentities&sites=enwiki&titles=Karachi&normalize=1
like image 42
Addshore Avatar answered Sep 28 '22 11:09

Addshore


I needed to do it in Google Sheets, and was able to do it with a formula like this. It assumes the Wikipedia title is in column B. Copy down to the rows you need

=IMPORTXML(concat("https://en.wikipedia.org/w/api.php?action=query&prop=pageprops&ppprop=wikibase_item&redirects=1&format=xml&titles=",B1),"//@wikibase_item")

Another suggestion is to use the dump https://dumps.wikimedia.org/enwiki/latest/enwiki-latest-page_props.sql.gz, 230Mb zipped

like image 40
Vladimir Alexiev Avatar answered Sep 28 '22 12:09

Vladimir Alexiev