Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Use Python Wikipedia-API to get text summary for any subject in a list of subjects
Requires Wikipedia-API 0.5.1 or greater - and Python 3
import wikipediaapi
w = wikipediaapi.Wikipedia('en')
p ='N._K._Jemisin')
# print exactly 2 sentences from summary
print(w.extracts(p, exsentences=2))
# print exactly 6 sentences from summary
# note - not all extracts have that many sentences
print(w.extracts(p, exsentences=6))
Requires Wikipedia-API and Python 3
import wikipediaapi
w = wikipediaapi.Wikipedia(language='en', extract_format=wikipediaapi.ExtractFormat.WIKI)
subjects = ['N. K. Jemisin', 'Cixin Liu', 'Ann Leckie', 'John Scalzi', 'Red G. Bloo', 'Jo Walton']
for subject in subjects:
p =
if p.exists():
print(p.summary, '\n')
print(p.fullurl, '\n')
print(subject + ": No information available.\n")

This comment has been minimized.

Copy link
Owner Author

commented Apr 6, 2019

Only way to control length of summary is with split: p.summary[0:60]

Regular API can get an extract, different from summary (shorter), but I haven't found a way to get this with Wikipedia-API.

Example: format=json&titles=Arundhati_Roy



This comment has been minimized.

Copy link
Owner Author

commented Apr 7, 2019

The package was updated April 7 to allow selection of extract length by number of sentences. @martin-majlis is amazing!


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.