Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 690
nlper Avatar asked Jun 02 '15 12:06

nlper


People also ask

How do I get an image from Wikipedia API?

Find the main image on a Wikipedia page and download it. Using a list of Wikipedia URLs, download the main image from each page in the list. Name the downloaded file to match the page URL. print("All done!")

Can you source images from Wikipedia?

You can use (free) images from Wikipedia on your own site, or anywhere you like. You can use images that are freely-licensed images, provided you comply with the individual image's license terms. While all article text is licensed under the GFDL, free images have several free content licenses to choose from.

Does Wiki have an API?

What is the Wikipedia API? The Wikipedia API (official documentation) is supported by the MediaWiki's API and provide access to Wikipedia and other MediaWiki data without interacting with the user interface.

How do I find an image on Wikipedia?

Check WikipediaCheck Wikimedia Commons, where images are sorted by category, a guide on finding images is available here. Check related articles on Wikipedia. Check What links here for articles that may contain images. Check foreign language links for the article and related articles, as they may have a photo already.


1 Answers

This returns the url of the main image of the article, not the random one:

import wikipedia
import requests
import json

WIKI_REQUEST = 'http://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles='

def get_wiki_image(search_term):
    try:
        result = wikipedia.search(search_term, results = 1)
        wikipedia.set_lang('en')
        wkpage = wikipedia.WikipediaPage(title = result[0])
        title = wkpage.title
        response  = requests.get(WIKI_REQUEST+title)
        json_data = json.loads(response.text)
        img_link = list(json_data['query']['pages'].values())[0]['original']['source']
        return img_link        
    except:
        return 0

wiki_image = get_wiki_image('Paris City')
like image 132
DmytroSytro Avatar answered Sep 26 '22 01:09

DmytroSytro