The wikipedia library in Python provides a simple and easy-to-use interface for accessing data from Wikipedia.
It acts as a wrapper around the Wikipedia API, allowing you to retrieve Wikipedia content such as
- articles, summaries, links, and more with minimal effort.
- Compared to using the inbuilt JSON lib.
- It abstracts the complexities of making HTTP requests to the API
The library allows you to:
- Search and fetch articles on/from Wikipedia.
- Get the full/extract content of a page or a random article.
- Handle disambiguation errors when multiple results are found.
- Work with content in multiple languages.
- Get additional metadata such as links, categories, and sections.
Before you can use the library you need to install the API Python Library:
You can install it using pip:
- pip install wikipedia
Here is the list of methods in the wikipedia Python library:
- wikipedia.search(query, results=10, suggestion=True)
- wikipedia.summary(title, sentences=1)
- wikipedia.page(title)
- wikipedia.random()
- wikipedia.set_lang(lang)
- wikipedia.set_rate_limit(True/False)
- wikipedia.page(title).content
- wikipedia.page(title).url
- wikipedia.page(title).title
- wikipedia.page(title).links
- wikipedia.page(title).categories
- wikipedia.page(title).sections
- wikipedia.page(title).images
- wikipedia.exceptions.DisambiguationError
- wikipedia.exceptions.RedirectError
- wikipedia.exceptions.HTTPTimeoutError
Fetching a random page using the wikipedia library in Python:
import wikipedia
# Fetch a random article title
random_article = wikipedia.random()
# Fetch the summary (extract) of the random article
extract = wikipedia.summary(random_article)
# Print the random article title and its summary
print(f"Random article title: {random_article}")
print(f"Extract: {extract}")
Main category