Python Wikipedia API

1.4.0 · maintenance · verified Sun Apr 12

The `wikipedia` library is a Pythonic wrapper that provides easy access to and parsing of data from Wikipedia. It allows users to search Wikipedia, retrieve article summaries, and extract structured data such as links and images from pages. The current stable version is 1.4.0. This library is designed for ease of use rather than advanced, high-volume scraping, and has not seen a release since 2014.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to search for Wikipedia pages, retrieve a concise summary, and access a full page object including its title and URL. It also includes basic error handling for common `DisambiguationError` and `PageError` exceptions.

import wikipedia

# Set language (optional, default is 'en')
wikipedia.set_lang("en")

# Search for pages
search_results = wikipedia.search("Artificial Intelligence")
print(f"Search results: {search_results[:3]}...")

# Get a summary of a page
try:
    summary_text = wikipedia.summary("Artificial intelligence", sentences=2)
    print(f"Summary: {summary_text}")
except wikipedia.exceptions.DisambiguationError as e:
    print(f"Disambiguation options: {e.options}")
    # Example of handling by picking the first option
    # print(f"Picking first option: {wikipedia.summary(e.options[0], sentences=2)}")
except wikipedia.exceptions.PageError:
    print("Page not found.")

# Get a full page object
try:
    page = wikipedia.page("Artificial intelligence")
    print(f"Page title: {page.title}")
    print(f"Page URL: {page.url}")
    # Access content, links, etc.
    # print(f"Page content (first 200 chars): {page.content[:200]}...")
    # print(f"Page links (first 5): {page.links[:5]}")
except wikipedia.exceptions.PageError:
    print("Page not found for full object.")
except wikipedia.exceptions.DisambiguationError as e:
    print(f"Disambiguation options for page: {e.options}")

view raw JSON →