Wikipedia-API
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
- breaking The `user_agent` parameter became mandatory in the `Wikipedia` and `AsyncWikipedia` constructors starting from version 0.6.0. Failing to provide it will raise an error.
- breaking The position of the `variant` parameter in the `Wikipedia` and `AsyncWikipedia` constructors changed to the third argument in versions 0.7.x. If you were explicitly passing `variant` by position in older versions, this will break.
- gotcha When using the `AsyncWikipedia` client, all data-fetching attributes (e.g., `summary`, `text`, `sections`, `links`, `fullurl`, `pageid`) are coroutines and must be `awaited`. Failing to do so will result in a coroutine object instead of the expected data.
- gotcha Providing an inappropriate or missing `User-Agent` string can lead to your requests being blocked by Wikimedia servers. It's essential to follow Wikimedia's User-Agent policy, including providing a project name and contact information.
- gotcha Excessive or rapid requests to the Wikipedia API, especially without proper delays, can lead to your IP being temporarily or permanently blocked by Wikimedia's servers due to rate limiting.
Install
-
pip install wikipedia-api
Imports
- Wikipedia
from wikipediaapi import Wikipedia
- AsyncWikipedia
from wikipediaapi import AsyncWikipedia
Quickstart
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.")