25

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.

Termininja
  • 6,620
  • 12
  • 48
  • 49
Shoussi Elbril
  • 261
  • 3
  • 5

3 Answers3

29

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.

Termininja
  • 6,620
  • 12
  • 48
  • 49
  • How would you do the opposite? That is, get the article when you just have the Wikidata ID? – t-bone Jun 27 '19 at 19:36
  • 1
    https://stackoverflow.com/questions/37079989/how-to-get-wikipedia-page-from-wikidata-id/37086915#37086915 – Termininja Nov 14 '19 at 08:29
  • It doesn't work for e.g. with Isabel_II_del_Reino_Unido – Leandro Bardelli Oct 24 '20 at 23:12
  • @Leandro, [Isabel II del Reino Unido](https://es.wikipedia.org/wiki/Isabel_II_del_Reino_Unido) is in Spanish not in English Wiki, and it is normal page, not redirection. But [Elizabeth II](https://es.wikipedia.org/w/index.php?title=Elizabeth_II&redirect=no) is redirection, so you can use https://es.wikipedia.org/w/api.php?action=query&prop=pageprops&ppprop=wikibase_item&redirects=1&titles=Elizabeth%20II or to get directly the wikidata id: https://es.wikipedia.org/w/api.php?action=query&prop=pageprops&ppprop=wikibase_item&redirects=1&titles=Isabel%20II%20del%20Reino%20Unido – Termininja Oct 29 '20 at 20:21
  • @Termininja yes thanks, I supossed that could find any entity by name without using the wiki lang specification – Leandro Bardelli Oct 29 '20 at 21:40
4

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:

Addshore
  • 568
  • 3
  • 16
  • do you have an example of an issue solved by the `normalize` parameter? – maxlath Mar 26 '17 at 21:43
  • 1
    Yes, a simple case would be titles=karachi will not return a result as the title is meant to be capitalized, normalize=1 will fix this. I will update the answer. – Addshore Dec 14 '17 at 16:20
  • If you mean "en" for English Wikipedia, all you need to do is change the "enwiki" part to some other wiki language, for example "dewiki" for German Wikipedia – Addshore Oct 26 '20 at 10:58
2

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

Vladimir Alexiev
  • 2,477
  • 1
  • 20
  • 31