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 Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read