1import wikipedia
2import requests
3import json
4
5WIKI_REQUEST = 'http://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles='
6
7def get_wiki_image(search_term):
8 try:
9 result = wikipedia.search(search_term, results = 1)
10 wikipedia.set_lang('en')
11 wkpage = wikipedia.WikipediaPage(title = result[0])
12 title = wkpage.title
13 response = requests.get(WIKI_REQUEST+title)
14 json_data = json.loads(response.text)
15 img_link = list(json_data['query']['pages'].values())[0]['original']['source']
16 return img_link
17 except:
18 return 0
19
20wiki_image = get_wiki_image('Paris City')
21