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.
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!")
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.
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.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With