The wikipedia Library in Python


python


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:

  1. wikipedia.search(query, results=10, suggestion=True)
  2. wikipedia.summary(title, sentences=1)
  3. wikipedia.page(title)
  4. wikipedia.random()
  5. wikipedia.set_lang(lang)
  6. wikipedia.set_rate_limit(True/False)
  7. wikipedia.page(title).content
  8. wikipedia.page(title).url
  9. wikipedia.page(title).title
  10. wikipedia.page(title).links
  11. wikipedia.page(title).categories
  12. wikipedia.page(title).sections
  13. wikipedia.page(title).images
  14. wikipedia.exceptions.DisambiguationError
  15. wikipedia.exceptions.RedirectError
  16. 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
Address

OpenSourceCook.in
"Natraj"  Bungalow,
Colony No.7,  Sr.No. 38.
(Lane Behind Sai Baba Mandir)
Kale Borate Nagar, Hadapsar,
Pune - 411028.
Get Directions