Skip to content

Instantly share code, notes, and snippets.

@Dineshkarthik
Created January 4, 2020 13:04
Show Gist options
  • Save Dineshkarthik/06dcbba20fabf31304558dccd47e111c to your computer and use it in GitHub Desktop.
Save Dineshkarthik/06dcbba20fabf31304558dccd47e111c to your computer and use it in GitHub Desktop.
Getting Wikipedia Article's summary using WIkimedia page content rest api and Random article generation
import requests
random_article = requests.get("https://en.wikipedia.org/wiki/Special:Random")
article_name = random_article.url.split("/")[-1]
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{article_name}"
article_data = requests.get(url).json()
print(article_data)
{'type': 'standard',
'title': 'Vohimasina Nord',
'displaytitle': 'Vohimasina Nord',
'namespace': {'id': 0, 'text': ''},
'wikibase_item': 'Q3562106',
'titles': {'canonical': 'Vohimasina_Nord',
'normalized': 'Vohimasina Nord',
'display': 'Vohimasina Nord'},
'pageid': 17242081,
'thumbnail': {'source': 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/42/Madagascar_location_map.svg/176px-Madagascar_location_map.svg.png',
'width': 176,
'height': 320},
'originalimage': {'source': 'https://upload.wikimedia.org/wikipedia/commons/4/42/Madagascar_location_map.svg',
'width': 803,
'height': 1458},
'lang': 'en',
'dir': 'ltr',
'revision': '771610717',
'tid': '57cd8e80-e88e-11e9-a507-c511d8eeb481',
'timestamp': '2017-03-22T15:05:24Z',
'description': 'Place in Vatovavy-Fitovinany, Madagascar',
'coordinates': {'lat': -21.73333333, 'lon': 48.13333333},
'content_urls': {'desktop': {'page': 'https://en.wikipedia.org/wiki/Vohimasina_Nord',
'revisions': 'https://en.wikipedia.org/wiki/Vohimasina_Nord?action=history',
'edit': 'https://en.wikipedia.org/wiki/Vohimasina_Nord?action=edit',
'talk': 'https://en.wikipedia.org/wiki/Talk:Vohimasina_Nord'},
'mobile': {'page': 'https://en.m.wikipedia.org/wiki/Vohimasina_Nord',
'revisions': 'https://en.m.wikipedia.org/wiki/Special:History/Vohimasina_Nord',
'edit': 'https://en.m.wikipedia.org/wiki/Vohimasina_Nord?action=edit',
'talk': 'https://en.m.wikipedia.org/wiki/Talk:Vohimasina_Nord'}},
'api_urls': {'summary': 'https://en.wikipedia.org/api/rest_v1/page/summary/Vohimasina_Nord',
'metadata': 'https://en.wikipedia.org/api/rest_v1/page/metadata/Vohimasina_Nord',
'references': 'https://en.wikipedia.org/api/rest_v1/page/references/Vohimasina_Nord',
'media': 'https://en.wikipedia.org/api/rest_v1/page/media/Vohimasina_Nord',
'edit_html': 'https://en.wikipedia.org/api/rest_v1/page/html/Vohimasina_Nord',
'talk_page_html': 'https://en.wikipedia.org/api/rest_v1/page/html/Talk:Vohimasina_Nord'},
'extract': 'Vohimasina Nord is a town and commune in Madagascar. It belongs to the district of Manakara, which is a part of Vatovavy-Fitovinany Region. The population of the commune was estimated to be approximately 22,000 in 2001 commune census.',
'extract_html': '<p><b>Vohimasina Nord</b> is a town and commune in Madagascar. It belongs to the district of Manakara, which is a part of Vatovavy-Fitovinany Region. The population of the commune was estimated to be approximately 22,000 in 2001 commune census.</p>'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment