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 (can be no greater than 10).
  • chars - if set, return only the first chars characters (actual text returned may be slightly longer).
  • auto_suggest - let Wikipedia find a valid page title for the query
  • redirect - allow redirection without raising RedirectError
wikipedia.page(title=None, pageid=None, auto_suggest=True, redirect=True, preload=False)

Get a WikipediaPage object for the page with title title or the pageid pageid (mutually exclusive).

Keyword arguments:

  • title - the title of the page to load
  • pageid - the numeric pageid of the page to load
  • 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
wikipedia.geosearch(latitude, longitude, title=None, results=10, radius=1000)

Do a wikipedia geo search for latitude and longitude using HTTP API described in http://www.mediawiki.org/wiki/Extension:GeoData

Arguments:

  • latitude (float or decimal.Decimal)
  • longitude (float or decimal.Decimal)

Keyword arguments:

  • title - The title of an article to search for
  • results - the maximum number of results returned
  • radius - Search radius in meters. The value must be between 10 and 10000
class wikipedia.WikipediaPage(title=None, pageid=None, redirect=True, preload=False, original_title=u'')

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

categories

List of categories of a page.

content

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

coordinates

Tuple of Decimals in the form of (lat, lon) or None

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.

parent_id

Revision ID of the parent version of the current revision of this page. See revision_id for more information.

references

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

revision_id

Revision ID of the page.

The revision ID is a number that uniquely identifies the current version of the page. It can be used to create the permalink or for other direct API calls. See Help:Page history for more information.

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.languages()

List all the currently supported language prefixes (usually ISO language code).

Can be inputted to set_lang to change the Mediawiki that wikipedia requests results from.

Returns: dict of <prefix>: <local_lang_name> pairs. To get just a list of prefixes, use wikipedia.languages().keys().

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(pageid=None, *args)

Exception raised when no Wikipedia matched a query.

exception wikipedia.exceptions.RedirectError(title)

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

exception wikipedia.exceptions.WikipediaException(error)

Base Wikipedia exception class.

Indices and tables