Search code examples
wikipediamediawiki-api

How to retrive some jpeg image from Wikipedia page by API?


For my app I need to extract images from the Wikipedia API. But I am not getting the correct API to get the jpeg image from main Wikipedia pages. I have tried the Sandbox API, but not succeeded yet. At present I have the following API request, which is not showing images according to my expectation. Is there any API request that I should use to get an image from the Wikipedia main page.

https://en.wikipedia.org/w/api.php?action=query&prop=imageinfo&iiprop=url&generator=images&iiurlwidth=400&generator=images&titles=Kiel

Solution

  • Most of the pages from Wikipedia are associated with an Item in Wikidata. If there is "main image" for some wiki item it will be kept by Image (P18) property. This property you can access by using MediaWiki API for Wikidata with wbgetentities action:

    https://www.wikidata.org/w/api.php?action=wbgetentities&sites=enwiki&titles=Paris
    

    In this example, the article Paris (titles=Paris) in English Wikipedia (sites=enwiki) will returns Wikidata Item (Q90) with its Image property. You can be more specific in request by using &props=claims to miss all unnecessary information like labels, descriptions, sitelinks etc. The result will include:

    {
        "entities": {
            "Q90": {
                "claims": {
                    "P18": [ {
                        "mainsnak": {
                            "datavalue": {
                                "value": "Paris - Eiffelturm und Marsfeld2.jpg",
                            },
                        },
                    } ],
                }
            }
        },
    }
    

    where the value "Paris - Eiffelturm und Marsfeld2.jpg" is the main image of the article.