Wikipedia module in Python
Wikipedia module in Python: The internet has become an essential part of all of our life which is the largest source of all the information. Therefore, it becomes very important for all of us to know that how we can get information or data from various sources through the internet.
- Among these various sources Wikipedia is the largest and most popular source for fetching most of the data and information from the internet.
- Wikipedia is the multilingual online (over the internet) Encyclopedia which was created and maintained by a non-profit community of volunteers as an open project. It is collaboration project that using a wiki-based editing system for maintenance.
Python's Wikipedia module
Since Wikipedia is the largest and most popular source of fetching all the information and data from the internet, Python developers had also developed the Wikipedia module for the Python language so that every user of Python can get the all the information of Wikipedia using Python script.
In this tutorial, we are going to discuss about the Wikipedia module of Python, how we can install it in our system, how can we use the Wikipedia module in our Python program and how can we fetch all the information from the Wikipedia through our Python script.
Installation of Python's Wikipedia Module
To extract, fetch data or information from the Wikipedia first we have to install the Python's Wikipedia module in our system. The Python's Wikipedia module is also called as Python Wikipedia library. The Python's Wikipedia module officially wraps all the official Wikipedia APIs.
We need to use the following command in our command prompt of system so that Python's Wikipedia module can easily be installed in our system:
pip install wikipedia

As we can see in the above image that we have successfully installed the Python's Wikipedia module in our system. Now we can start working with Python's Wikipedia module in our Python program.
Getting Started with Python's Wikipedia module
In this tutorial, we will learn how we can get information from Wikipedia using Wikipedia module in our Python program. We can fetch the data from Wikipedia using Python program by the following two ways:
1. Getting the summary or limited information from a given title in our Python program
2. Getting a full page of information from Wikipedia through our Python program
3. Getting the suggestions of title from the given title in our Python program:
We will understand all methods given above one by one in detail and learn which syntax we need to use in each method
1. Getting the summary or limited information from a given title in our Python program:
In this method, we will get only limited information or summary of the title that given in our Python program from Wikipedia using the Python's Wikipedia module in our code.
Syntax:
We use the following syntax in our Python program so that we can access only the summary of title from Wikipedia:
wikipedia.summary(title_of_the_topic, number_of_sentences)
Arguments in syntax:
- title_of_the_topic – It is a title of the topic which we want to search in Wikipedia through our Python program
- number_of_sentences – It is the number of sentences we want to get in the result of the search as in our output
Result we get: The number of sentences we have mentioned in the second argument of the syntax given above is the result we will get through it in the string format as in the output.
We have learned the syntax to use this method for getting only summary of the title we given in our Python program, now we will use the syntax in some examples so we can understand this method in better way.
Example 1: Look at the following Python program:
# importing the wikipedia module in our Python program import wikipedia # using the syntax for finding result for the search Chinese word # sentences = 5 is referring to numbers of line we want in output SearchResult = wikipedia.summary("China", sentences = 5) # printing the result of the search using print command print(SearchResult)
Output:

Explanation: As in the output, we can see that we have first imported Wikipedia module in our Python program. Then, we defined the variable name SearchResult to use it in print statement. Then we searched for the Chinese word from the Wikipedia using wikipedia.summary() syntax and the result is stored in SearchResult variable. We will get only the number of sentences we mentioned in second argument of the syntax as result. Then we printed the result of the search using the SearchResult variable in the print statement.
Example 2: Look at the following Python program:
# importing the wikipedia module in our Python program import wikipedia # using the syntax for finding result for the search Python word # sentences = 4 is referring to numbers of line we want in output Result_of_search = wikipedia.summary("Python", sentences = 4) # printing the result of the search using print command print(Result_of_search)
Output:

Explanation: As in the above given program we can see that we have first imported Wikipedia module in our Python program. Then, we defined the variable name Result_of_search to use it in print statement.
Then we searched for the Python word from the Wikipedia using wikipedia.search() syntax and the result is stored in Result_of_search variable. We will get only the number of sentences i.e., 4 sentences we mentioned in second argument of the syntax as result. Then we printed the result of the search using the Result_of_search variable in the print statement.
2. Getting a full page of information from Wikipedia through our Python program:
In this method, we will get full page information of the title that given in our Python program from Wikipedia using the Python's Wikipedia module.
Syntax
We need to use the following syntax in our Python program so that we can access the full-page information of title from Wikipedia:
wikipedia.page(title_of_the_topic)
Arguments in syntax:
- Title of the topic which we want to search in Wikipedia through our Python program
Result we get: In the result of this syntax, we will get full-page information of the title as in the output that we have mentioned in the argument of the syntax.
We have learned the syntax to use this method for getting the full-page information of the title we given in our Python program, now we will use the syntax in some examples so we can understand this method in better way.
Example - Following are the example where we will use the above method to fetch full page information of the title through Wikipedia:
# importing the wikipedia module in our program import wikipedia # creating a wikipedia page object to store the result of title page_object = wikipedia.page("Python") # printing html version of page_object created in result print(page_object.html) # printing title we have used in the program print(page_object.original_title) # printing links that are present on that page object print(page_object.links[0:10])
Output:
<bound method WikipediaPage.html of <WikipediaPage 'Python (programming language)'>> Python (programming language) ['"Hello, World!" program', '3ds Max', '?:', 'ABC (programming language)', 'ADMB', 'ALGOL', 'ALGOL 68', 'APL (programming language)', 'Abaqus', 'Academic Free License']
Explanation:
In the above given program, we have imported Wikipedia module in first line of code. Then, we created a page object where we will store the result of that program and then after using the syntax, we have printed the result page in html version through page object. Then we printed title we given and links present in the result.
3. Getting the suggestions of title from the given title in our Python program:
In this method, we will get suggestions of the title from the title that given in our Python program from Wikipedia using the Python's Wikipedia module in our code.
Syntax
We need to use the following syntax in our Python program so that we can access the suggestions of title from given title from Wikipedia:
wikipedia.search(title_of_the_topic, Number_of_result_we_want)
Arguments in Syntax
Following two argument we have to give in the above syntax while using it in our Python program:
- Title of the topic which we want to search in Wikipedia through our Python program
- Number of results that means total suggestions with the title we want in our result
Result we get: In the result of this syntax, we will get suggestions of the title from the given title in our program as in the output that we have mentioned in the argument of the syntax.
We have learned the syntax to use this method for getting the suggestions of the title from the given title we given in our Python program, now we will use the syntax in some examples so we can understand this method in better way.
Example - Following are the example where we will use the above method to fetch suggestions of the title through Wikipedia:
# importing the wikipedia module in our program import wikipedia # getting suggestions of title with related to our title SearchResult = wikipedia.search("Tutorial", results = 6) # printing the result of the search in the output print(SearchResult)
Output:
['Tutorial', 'Daodong Tutorial Academy', 'Tutorial system', 'Tutorial (comedy duo)', 'Pingtung Tutorial Academy', 'Fongyi Tutorial Academy']
Explanation: In the above given program, we have imported Wikipedia module in our program in first line of code. Then, we created a Search Result variable where we will store result of the search for the title that we have used in the argument. Then we will print the result in the output of the program. We will get only number of results for the suggestion of title that we have mentioned in the second argument of the given syntax in the program.