Wikipedia module in Python
Last Updated :
28 Apr, 2025
The Internet is the single largest source of information, and therefore it is important to know how to fetch data from various sources. And with Wikipedia being one of the largest and most popular sources for information on the Internet.
Wikipedia is a multilingual online encyclopedia created and maintained as an open collaboration project by a community of volunteer editors using a wiki-based editing system.
In this article, we will see how to use Python's Wikipedia module to fetch a variety of information from the Wikipedia website.
Installation
In order to extract data from Wikipedia, we must first install the Python Wikipedia library, which wraps the official Wikipedia API. This can be done by entering the command below in your command prompt or terminal:
pip install wikipedia
Getting Started
Getting the summary of any title
Summary of any title can be obtained by using summary method.
Syntax : wikipedia.summary(title, sentences)
Argument :
Title of the topic
Optional argument: setting number of lines in result.
Return : Returns the summary in string format.
Code :
Python3
# importing the module
import wikipedia
# finding result for the search
# sentences = 2 refers to numbers of line
result = wikipedia.summary("India", sentences = 2)
# printing the result
print(result)
Output :
India (Hindi: Bh?rat), officially the Republic of India (Hindi: Bh?rat Ga?ar?jya), is a country in South Asia. It is the seventh-largest country by area, the second-most populous country, and the most populous democracy in the world.
Searching title and suggestions
Title and suggestions can be get by using search() method.
Syntax : wikipedia.search(title, results)
Argument :
Title of the topic
Optional argument : setting number of result.
Return : Returns the list of titles.
Code :
Python3
# importing the module
import wikipedia
# getting suggestions
result = wikipedia.search("Geek", results = 5)
# printing the result
print(result)
Output :
['Geek', 'Geek!', 'Freaks and Geeks', 'The Geek', 'Geek show']
Getting Full Wikipedia Page Data
The page() method is used to get the contents, categories, coordinates, images, links and other metadata of a Wikipedia page.
Syntax : wikipedia.page(title)
Argument : Title of the topic.
Return : Return a WikipediaPage object.
Code :
Python3
# importing the module
import wikipedia
# wikipedia page object is created
page_object = wikipedia.page("india")
# printing html of page_object
print(page_object.html)
# printing title
print(page_object.original_title)
# printing links on that page object
print(page_object.links[0:10])
Output :
"bound method WikipediaPage.html of "WikipediaPage 'India'">
India
['.in', '10th BRICS summit', '11th BRICS summit', '12th BRICS summit', '17th SAARC summit', '18th SAARC summit', '1951 Asian Games', '1957 Indian general election', '1962 Indian general election', '1982 Asian Games']
Changing language of Wikipedia page
The language can be changed to your native language if the page exists in your native language. Set_lang() method is used for the same.
Syntax : wikipedia.set_lang(language)
Argument : prefix of the language like for arabic prefix is ar and so on.
Action performed : It converted the data into that language default language is english.
Code :
Python3
# importing the module
import wikipedia
# setting language to hindi
wikipedia.set_lang("hi")
# printing the summary
print(wikipedia.summary("India"))
Output :
Similar Reads
Python Module Index Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
Python String Module The string module is a part of Python's standard library and provides several helpful utilities for working with strings. From predefined sets of characters (such as ASCII letters, digits and punctuation) to useful functions for string formatting and manipulation, the string module streamlines vario
4 min read
External Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external m
5 min read
PyDictionary module in Python AyDictionary It's a Python module used to fetch definitions of words, while googletrans provides translation services.meaningstranslationsInstallation To install AyDictionary run the following pip code on the terminal / command prompt: pip install AyDictionary googletrans==4.0.0-rc1Getting Started N
1 min read
Fetching text from Wikipedia's Infobox in Python An infobox is a template used to collect and present a subset of information about its subject. It can be described as a structured document containing a set of attribute-value pairs, and in Wikipedia, it represents a summary of information about the subject of an article.So a Wikipedia infobox is a
3 min read
Why is Python So Popular? One question always comes into people's minds Why Python is so popular? As we know Python, the high-level, versatile programming language, has witnessed an unprecedented surge in popularity over the years. From web development to data science and artificial intelligence, Python has become the go-to
7 min read