Registry / http-networking / wikipedia

wikipedia

JSON →
library1.4.0pypypi✓ verified 23d ago

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.

pip install wikipedia
INSTALL
IMPORT
SIG · WIKIPEDIA
W
wikipedia
http-networkingpythonv1.4.0
Install
3.2s avg
Import
518ms
Disk
22MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.536s · 23.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.2s · import 0.500s · 24MB
22MB installed
● package 22MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

wikipedia
import wikipedia
The primary module for interacting with the Wikipedia API.
DisambiguationError
from wikipedia.exceptions import DisambiguationError
Required for handling cases where a search query matches multiple Wikipedia pages.
PageError
from wikipedia.exceptions import PageError
Required for handling cases where a search query does not match any Wikipedia pages.

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}")
Debug
Known issues
gotchaCalling `wikipedia.summary()` or `wikipedia.page()` with an ambiguous query (e.g., 'Mercury') will raise a `wikipedia.exceptions.DisambiguationError`.
fix
Always wrap calls to `summary()` and `page()` in a `try-except` block to catch `DisambiguationError` and either present options to the user or programmatically select one from `e.options`.
affects: 1.x.x
gotchaIf a query does not match any Wikipedia page, `wikipedia.summary()` or `wikipedia.page()` will raise a `wikipedia.exceptions.PageError`.
fix
Catch `wikipedia.exceptions.PageError` in your `try-except` blocks and handle cases where no matching page is found, e.g., by suggesting alternative queries.
affects: 1.x.x
gotchaThe library's default `auto_suggest=True` behavior can sometimes silently correct a query to an unintended or incorrect page, leading to unexpected results or `PageError` for what seems like a valid query. For example, 'Commander Worf' might become 'commander wharf'.
fix
For precise queries, consider setting `auto_suggest=False` in `wikipedia.page()` and `wikipedia.summary()` calls, and handle `PageError` explicitly if the exact page isn't found.
affects: 1.x.x
deprecatedThis `wikipedia` library (goldsmith/Wikipedia) has not been updated since November 2014 (version 1.4.0). While still functional, it may not support the latest Wikipedia API features, might have unaddressed bugs, or could eventually break due to API changes.
fix
For new projects or if encountering issues, consider evaluating alternative, more actively maintained Python wrappers for the Wikipedia API, such as `wikipedia-api` (martin-majlis/Wikipedia-API).
affects: 1.4.0
gotchaThis library is designed for simple, casual use. It does not include features like rate limiting, robust error handling for network issues, or extensive scraping capabilities. Using it for high-volume or aggressive scraping can lead to IP blocking or violations of Wikimedia's terms of service.
fix
For serious scraping, automated requests, or editing, use more advanced MediaWiki API wrappers like Pywikibot, which offer rate limiting and other features for considerate interaction with Wikimedia infrastructure.
affects: 1.x.x
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'wikipedia'
The 'wikipedia' library is not installed in your current Python environment.
fix
Run 'pip install wikipedia' in your terminal to install the library.
wikipedia.exceptions.DisambiguationError: "Your Search Term" may refer to: ...
Your search query matched multiple Wikipedia articles, leading to a disambiguation page, and the library cannot automatically choose one.
fix
Handle the `DisambiguationError` by choosing one of the options provided in the error message or refining your search term.
```python
import wikipedia
try:
    page = wikipedia.page("Python")
except wikipedia.exceptions.DisambiguationError as e:
    print(f"Ambiguous term. Options: {e.options}")
    # To resolve, pick one, e.g., the first option:
    # page = wikipedia.page(e.options[0])
```
wikipedia.exceptions.PageError: "NonExistentPageName" does not exist.
The requested Wikipedia page title does not exist or cannot be found by the library.
fix
Catch the `PageError` exception and check the spelling of your page title, or use `wikipedia.search()` to find alternative titles.
```python
import wikipedia
try:
    page = wikipedia.page("DefinitelyNotARealPageTitle12345")
except wikipedia.exceptions.PageError:
    print("The requested Wikipedia page does not exist. Please check the title.")
    # You might try:
    # print(wikipedia.search("SimilarTerm"))
```
IndexError: list index out of range
You attempted to access an element from the list returned by `wikipedia.search()`, but the list was empty because no results were found.
fix
Always check if the list returned by `wikipedia.search()` is not empty before attempting to access its elements.
```python
import wikipedia
results = wikipedia.search("A search term with no results")
if results:
    first_result = results[0]
    print(f"First result: {first_result}")
else:
    print("No search results found for the query.")
```
wikipedia.exceptions.HTTPTimeoutError: Request timed out.
The request to the Wikipedia API took too long to respond, possibly due to network issues, a slow API, or a very large request.
fix
Increase the timeout parameter for your requests using `wikipedia.set_timeout()` or check your network connectivity.
```python
import wikipedia
wikipedia.set_timeout(30) # Increase timeout to 30 seconds (default is 10)
try:
    page = wikipedia.page("Long Article Name")
except wikipedia.exceptions.HTTPTimeoutError:
    print("Request to Wikipedia timed out. Check network or increase timeout.")
```
Upgrade
Version history
1.4.0latest on PyPI · released Nov 15, 2014
Audit
Dependencies

No dependency data recorded yet.

Agent activity
45 hits · last 30 days
node
36
OpenAI (training)
1
Resources
wikipedia — pip install wikipedia · libregistry