Skip to main content

Python Wrapper for Wikipedia

Project description

Wikipedia API

Wikipedia-API is easy to use Python wrapper for Wikipedias’ API. It supports extracting texts, sections, links, categories, translations, etc from Wikipedia. Documentation provides code snippets for the most common use cases.

build status Documentation Status Test Coverage Version Py Versions GitHub stars


This package requires at least Python 3.4 to install because it’s using IntEnum.

pip3 install wikipedia-api


Goal of Wikipedia-API is to provide simple and easy to use API for retrieving informations from Wikipedia. Bellow are examples of common use cases.


import wikipediaapi

How To Get Single Page

Getting single page is straightforward. You have to initialize Wikipedia object and ask for page by its name. To initialize it, you have to provide:

  • user_agent to identify your project. Please follow the recommended format.

  • language to specify language mutation. It has to be one of supported languages.

import wikipediaapi
    wiki_wiki = wikipediaapi.Wikipedia('MyProjectName (', 'en')

    page_py ='Python_(programming_language)')

How To Check If Wiki Page Exists

For checking, whether page exists, you can use function exists.

page_py ='Python_(programming_language)')
print("Page - Exists: %s" % page_py.exists())
# Page - Exists: True

page_missing ='NonExistingPageWithStrangeName')
print("Page - Exists: %s" %     page_missing.exists())
# Page - Exists: False

How To Get Page Summary

Class WikipediaPage has property summary, which returns description of Wiki page.

import wikipediaapi
    wiki_wiki = wikipediaapi.Wikipedia('MyProjectName (', 'en')

    print("Page - Title: %s" % page_py.title)
    # Page - Title: Python (programming language)

    print("Page - Summary: %s" % page_py.summary[0:60])
    # Page - Summary: Python is a widely used high-level programming language for

How To Get Page URL

WikipediaPage has two properties with URL of the page. It is fullurl and canonicalurl.



How To Get Full Text

To get full text of Wikipedia page you should use property text which constructs text of the page as concatanation of summary and sections with their titles and texts.

wiki_wiki = wikipediaapi.Wikipedia(
    user_agent='MyProjectName (',

p_wiki ="Test 1")
# Summary
# Section 1
# Text of section 1
# Section 1.1
# Text of section 1.1
# ...

wiki_html = wikipediaapi.Wikipedia(
    user_agent='MyProjectName (',
p_html ="Test 1")
# <p>Summary</p>
# <h2>Section 1</h2>
# <p>Text of section 1</p>
# <h3>Section 1.1</h3>
# <p>Text of section 1.1</p>
# ...

How To Get Page Sections

To get all top level sections of page, you have to use property sections. It returns list of WikipediaPageSection, so you have to use recursion to get all subsections.

def print_sections(sections, level=0):
        for s in sections:
                print("%s: %s - %s" % ("*" * (level + 1), s.title, s.text[0:40]))
                print_sections(s.sections, level + 1)

# *: History - Python was conceived in the late 1980s,
# *: Features and philosophy - Python is a multi-paradigm programming l
# *: Syntax and semantics - Python is meant to be an easily readable
# **: Indentation - Python uses whitespace indentation, rath
# **: Statements and control flow - Python's statements include (among other
# **: Expressions - Some Python expressions are similar to l

How To Get Page Section By Title

To get last section of page with given title, you have to use function section_by_title. It returns the last WikipediaPageSection with this title.

section_history = page_py.section_by_title('History')
print("%s - %s" % (section_history.title, section_history.text[0:40]))

# History - Python was conceived in the late 1980s b

How To Get All Page Sections By Title

To get all sections of page with given title, you have to use function sections_by_title. It returns the all WikipediaPageSection with this title.

    page_1920 ='1920')
    sections_january = page_1920.sections_by_title('January')
    for s in sections_january:
        print("* %s - %s" % (s.title, s.text[0:40]))

# * January - January 1
# Polish–Soviet War in 1920: The
# * January - January 2
# Isaac Asimov, American author
# * January - January 1 – Zygmunt Gorazdowski, Polish

How To Get Page In Other Languages

If you want to get other translations of given page, you should use property langlinks. It is map, where key is language code and value is WikipediaPage.

def print_langlinks(page):
        langlinks = page.langlinks
        for k in sorted(langlinks.keys()):
            v = langlinks[k]
            print("%s: %s - %s: %s" % (k, v.language, v.title, v.fullurl))

# af: af - Python (programmeertaal):
# als: als - Python (Programmiersprache):
# an: an - Python:
# ar: ar - بايثون:
# as: as - পাইথন:

page_py_cs = page_py.langlinks['cs']
print("Page - Summary: %s" % page_py_cs.summary[0:60])
# Page - Summary: Python (anglická výslovnost [ˈpaiθtən]) je vysokoúrovňový sk

How To Get Page Categories

If you want to get all categories under which page belongs, you should use property categories. It’s map, where key is category title and value is WikipediaPage.

def print_categories(page):
        categories = page.categories
        for title in sorted(categories.keys()):
            print("%s: %s" % (title, categories[title]))

# Category:All articles containing potentially dated statements: ...
# Category:All articles with unsourced statements: ...
# Category:Articles containing potentially dated statements from August 2016: ...
# Category:Articles containing potentially dated statements from March 2017: ...
# Category:Articles containing potentially dated statements from September 2017: ...

How To Get All Pages From Category

To get all pages from given category, you should use property categorymembers. It returns all members of given category. You have to implement recursion and deduplication by yourself.

def print_categorymembers(categorymembers, level=0, max_level=1):
        for c in categorymembers.values():
            print("%s: %s (ns: %d)" % ("*" * (level + 1), c.title, c.ns))
            if c.ns == wikipediaapi.Namespace.CATEGORY and level < max_level:
                print_categorymembers(c.categorymembers, level=level + 1, max_level=max_level)

cat ="Category:Physics")
print("Category members: Category:Physics")

# Category members: Category:Physics
# * Statistical mechanics (ns: 0)
# * Category:Physical quantities (ns: 14)
# ** Refractive index (ns: 0)
# ** Vapor quality (ns: 0)
# ** Electric susceptibility (ns: 0)
# ** Specific weight (ns: 0)
# ** Category:Viscosity (ns: 14)
# *** Brookfield Engineering (ns: 0)

How To See Underlying API Call

If you have problems with retrieving data you can get URL of undrerlying API call. This will help you determine if the problem is in the library or somewhere else.

import wikipediaapi
import sys

# Set handler if you use Python in interactive mode
out_hdlr = wikipediaapi.logging.StreamHandler(sys.stderr)
out_hdlr.setFormatter(wikipediaapi.logging.Formatter('%(asctime)s %(message)s'))

wiki = wikipediaapi.Wikipedia(user_agent='MyProjectName (', language='en')

page_ostrava ='Ostrava')
# logger prints out: Request URL:

Other Badges

Code Climate Issue Count Coveralls Version Py Versions implementations Downloads Tags github-release Github commits (since latest release) GitHub forks GitHub stars GitHub watchers GitHub commit activity the past week, 4 weeks, year Last commit GitHub code size in bytes GitHub repo size in bytes PyPi License PyPi Wheel PyPi Format PyPi PyVersions PyPi Implementations PyPi Status PyPi Downloads - Day PyPi Downloads - Week PyPi Downloads - Month - SourceRank - Dependent Repos

Other Pages



  • Adds support for retrieving all sections with given name - Issue 39


  • Namespace could be arbitrary integer - Issue 29


  • Adds persistent HTTP connection - Issue 26
    • Downloading 50 pages reduced from 13s to 8s => 40% speed up



  • Adds tox for testing different Python versions


  • Allows modifying API call parameters

  • Fixes Issue 16 - hidden categories

  • Fixes Issue 21 - summary extraction


  • Handles missing sections correctly

  • Fixes Issue 20


  • Uses HTTPS directly instead of HTTP to avoid redirect


  • Correctly extracts text from pages without sections

  • Adds support for quoted page titles

api = wikipediaapi.Wikipedia(
python = api.article(


  • Adds support for Python 3.4 by not using f-strings


  • Uses code style enforced by flake8

  • Increased code coverage


  • Uses type annotations => minimal requirement is now Python 3.5

  • Adds possibility to use more parameters for request. For example:

api = wikipediaapi.Wikipedia(
    proxies={'http': 'http://localhost:1234'}
  • Extends documentation





  • Removing WikipediaLangLink

  • Page keeps track of its own language, so it’s easier to jump between different translations of the same page


  • Rename directory from wikipedia to wikipediaapi to avoid collisions


  • Handle redirects properly


  • Usage method page instead of article in Wikipedia




  • Use properties instead of functions

  • Added support for property Info


  • Support for extracting texts with HTML markdown

  • Added initial version of unit tests


  • It’s possible to extract summary and sections of the page

  • Added support for property Extracts

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Wikipedia-API-0.6.0.tar.gz (17.2 kB view hashes)

Uploaded source

Built Distribution

Wikipedia_API-0.6.0-py3-none-any.whl (14.4 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page