How to reliably get the image used in the Wikipedia Infobox?
Asked Answered
J

1

4

How do I (reliably) get the main image(s) used in the Wikipedia Infobox from the API?

This question has been asked before and the accepted answer admits that it is just a guess. Subsequent answers seem like a hack, at best and don't return the correct image.

For instance, the Jimi Hendrix Wikipedia entry uses "File:Jimi Hendrix 1967.png" as the main image in the InfoBox.

The updated answers suggest using this url but for Jimi Hendrix (and other topics) it often returns the wrong image.

If I pull in all the images there is no way to determine which is the image used in the Infobox.

Joscelin answered 23/4, 2016 at 16:25 Comment(0)
P
5

Each Wikipedia page (e.g. Jimi Hendrix) is associated with an Wikidata item ID (Q5928). The main image for each Wikipedia article (usually this in the Infobox template) is kept by image (P18) property in Wikidata, and you can access it by MediaWiki Wikidata API:

https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&sites=enwiki&props=claims&titles=Jimi Hendrix

With this query you will get the Wikidata ID and the image name:

{
    ...
    "id": "Q5928", // Wikidata ID
    "claims":{
        ...
        "P18":[{
            "mainsnak":{
                "datavalue":{
                    "value":"Jimi Hendrix 1967.png", // The image name
                },
            },
        }],
        ...
    }
}

And here I explain how to get the image URL also by using Wikidata ID.

Paroxysm answered 23/4, 2016 at 17:30 Comment(5)
I think that will work...thx! Is there anyway to return the first paragraph of the article w/ this query or do I need to do 2 queries? I'd like to make less API calls if possible.Joscelin
WikiData is a repository for facts, so no, for that you'll have to query WikipediaBateau
@kristen, in this case it is better to use #35663729Paroxysm
A lot of entries in wikidata do not provide the image. For example wikidata.org/wiki/Q588420 does not have the P18 property but it does have an image in the english wikipedia: en.wikipedia.org/wiki/Magnolia_(film)Libyan
@GuillemPoy, this is because these images are not free and copyrighted.Paroxysm

© 2022 - 2024 — McMap. All rights reserved.