How to reliably get the image used in the Wikipedia Infobox?

大憨熊 提交于 2021-02-07 22:42:20

问题


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.


回答1:


Each Wikipedia page (for example Jimi Hendrix) is associated with an Wikidata item (Q5928). The main image for each Wikipedia article (usually this in the Infobox template) is kept by image (P18) property in Wikidata. Because all Wikipedia templates parameters will be moved to Wikidata very soon, I suggest you to start to use 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 a lot of other information also, but and the main article image name:

{
    ...
    "claims":{
        ...
        "P18":[{
            "mainsnak":{
                "datavalue":{
                    "value":"Jimi Hendrix 1967.png",
                },
            },
        }],
        ...
    }
}

And here I explain how to get the image URL also.



来源:https://stackoverflow.com/questions/36813352/how-to-reliably-get-the-image-used-in-the-wikipedia-infobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!