Wikipedia Documentation

Here you can find the full developer API for the wikipedia project.

Contents:

Functions and Classes

wikipedia.search(query, results=10, suggestion=False)

Do a Wikipedia search for query.

Keyword arguments:

  • results - the maxmimum number of results returned
  • suggestion - if True, return results and suggestion (if any) in a tuple
wikipedia.suggest(query)

Get a Wikipedia search suggestion for query. Returns a string or None if no suggestion was found.

wikipedia.summary(query, sentences=0, chars=0, auto_suggest=True, redirect=True)

Plain text summary of the page.

Note

This is a convenience wrapper - auto_suggest and redirect are enabled by default

Keyword arguments:

  • sentences - if set, return the first sentences sentences
  • chars - if set, return only the first chars characters.
  • auto_suggest - let Wikipedia find a valid page title for the query
  • redirect - allow redirection without raising RedirectError
wikipedia.page(title, auto_suggest=True, redirect=True, preload=False)

Get a WikipediaPage object for the page with title title.

Keyword arguments:

  • auto_suggest - let Wikipedia find a valid page title for the query
  • redirect - allow redirection without raising RedirectError
  • preload - load content, summary, images, references, and links during initialization
class wikipedia.WikipediaPage(title, redirect=True, preload=False, original_title='')

Contains data from a Wikipedia page. Uses property methods to filter data from the raw HTML.

content

Plain text content of the page, excluding images, tables, and other data.

html()

Get full page HTML.

Warning

This can get pretty slow on long pages.

images

List of URLs of images on the page.

List of titles of Wikipedia page links on a page.

Note

Only includes articles from namespace 0, meaning no Category, User talk, or other meta-Wikipedia pages.

load(redirect=True, preload=False)

Load basic information from Wikipedia. Confirm that page exists and is not a disambiguation/redirect.

Does not need to be called manually, should be called automatically during __init__.

references

List of URLs of external links on a page. May include external links within page that aren’t technically cited anywhere.

section(section_title)

Get the plain text content of a section from self.sections. Returns None if section_title isn’t found, otherwise returns a whitespace stripped string.

This is a convenience method that wraps self.content.

Warning

Calling section on a section that has subheadings will NOT return the full text of all of the subsections. It only gets the text between section_title and the next subheading, which is often empty.

sections

List of section titles from the table of contents on the page.

summary

Plain text summary of the page.

wikipedia.set_lang(prefix)

Change the language of the API being requested. Set prefix to one of the two letter prefixes found on the list of all Wikipedias.

After setting the language, the cache for search, suggest, and summary will be cleared.

Note

Make sure you search for page titles in the language that you have set.

wikipedia.set_rate_limiting(rate_limit, min_wait=datetime.timedelta(0, 0, 50000))

Enable or disable rate limiting on requests to the Mediawiki servers. If rate limiting is not enabled, under some circumstances (depending on load on Wikipedia, the number of requests you and other wikipedia users are making, and other factors), Wikipedia may return an HTTP timeout error.

Enabling rate limiting generally prevents that issue, but please note that HTTPTimeoutError still might be raised.

Arguments:

  • rate_limit - (Boolean) whether to enable rate limiting or not

Keyword arguments:

  • min_wait - if rate limiting is enabled, min_wait is a timedelta describing the minimum time to wait before requests.

    Defaults to timedelta(milliseconds=50)

wikipedia.random(pages=1)

Get a list of random Wikipedia article titles.

Note

Random only gets articles from namespace 0, meaning no Category, User talk, or other meta-Wikipedia pages.

Keyword arguments:

  • pages - the number of random pages returned (max of 10)
wikipedia.donate()

Open up the Wikimedia donate page in your favorite browser.

Exceptions

Global wikipedia exception and warning classes.

exception wikipedia.exceptions.DisambiguationError(title, may_refer_to)

Exception raised when a page resolves to a Disambiguation page.

The options property contains a list of titles of Wikipedia pages that the query may refer to.

Note

options does not include titles that do not link to a valid Wikipedia page.

exception wikipedia.exceptions.HTTPTimeoutError(query)

Exception raised when a request to the Mediawiki servers times out.

exception wikipedia.exceptions.PageError(page_title)

Exception raised when no Wikipedia matched a query.

exception wikipedia.exceptions.RedirectError(page_title)

Exception raised when a page title unexpectedly resolves to a redirect.

exception wikipedia.exceptions.WikipediaException(error)

Base Wikipedia exception class.

Indices and tables