Wikipedia-API

0.13.0 · active · verified Wed Apr 01

Wikipedia-API is a Python wrapper that provides an easy-to-use interface for interacting with Wikipedia's MediaWiki API. It supports extracting various types of information, including text, sections, links, categories, and translations from Wikipedia pages. The library offers both synchronous and asynchronous clients for flexible integration, and is actively maintained with frequent releases, currently at version 0.13.0.

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize the synchronous Wikipedia client, retrieve a page by title, check its existence, and access its summary, URL, and sections. A descriptive user_agent is mandatory.

import wikipediaapi
import os

# It's crucial to provide a User-Agent that identifies your project.
# Replace 'MyProjectName (contact@example.com)' with your actual project name and contact info.
# See https://meta.wikimedia.org/wiki/User-Agent_policy
wiki_wiki = wikipediaapi.Wikipedia(
    user_agent=os.environ.get('WIKIPEDIA_USER_AGENT', 'MyPythonApp/1.0 (contact@example.com)'),
    language='en'
)

page_py = wiki_wiki.page('Python (programming language)')

if page_py.exists():
    print(f"Page title: {page_py.title}")
    print(f"Summary: {page_py.summary[0:200]}...")
    print(f"Full URL: {page_py.fullurl}")
    # Example of accessing sections
    for s in page_py.sections:
        print(f"Section: {s.title}")
        break # Just print the first section title for brevity
else:
    print(f"Page 'Python (programming language)' does not exist.")

view raw JSON →