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 wikipediaVerified import paths — ran on the pinned version, not inferred.
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.
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`.
Catch `wikipedia.exceptions.PageError` in your `try-except` blocks and handle cases where no matching page is found, e.g., by suggesting alternative queries.
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.
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).
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.
Run 'pip install wikipedia' in your terminal to install the library.
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])
```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"))
```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.")
```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.")
```No dependency data recorded yet.