Search code examples
pythonwikipediadbpedia

is there any API to get image from wiki page


I want to get main image from wikipedia page, I have all wikipedia entity name from which I create wiki link and getting main image from that page.

I tried with

https://github.com/richardasaurus/wiki-api, https://github.com/goldsmith/Wikipedia

But this does not work on all pages though page contains image.

from wikiapi import WikiApi
wiki = WikiApi()
wiki = WikiApi({ 'locale' : 'es'})
def getWikiImage(entity):
    results = wiki.find(entity)
    print results
    if len(results):
        article = wiki.get_article(results[0])
        print article.image
#getWikiImage("Rudy Sarzo")
getWikiImage("Melody Gersbach")

mediawiki api at http://www.mediawiki.org/wiki/API:Client_code#Python I checked out, but does not seem to help.


Solution

  • Here is little example how to get wikipage and main image should be the first one on the page.

    import wikipedia
    
    PAGES = ['New York', 'Mercury_(planet)', 'Tucana']
    
    for page in PAGES:
        wikipage = wikipedia.page(page)
        print "Page Title: %s" % wikipage.title
        print "Page URL: %s" % wikipage.url
        print "Nr. of images on page: %d" % len(wikipage.images)
        print " - Main Image: %s" % wikipage.images[0]
        print ""
    

    Checkout out Quickstart guide for Wikipedia API.