×

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
Wikipedia module in Python

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:

Wikipedia module in Python

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:

Wikipedia module in Python

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.


Related Topics

Python Argmin

Introduction The argmin function is defined as numpy.argmin(). This function returns the index of the minimum value or element from a Numpy array in a specific axis. An array is taken...

3 minutes read.

Python Unit Test Cheat String

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Printf Style Formating

String objects have one special implicit activity: the % administrator (modulo). This is otherwise called the string designing or interjection administrator. Given design % values (where configuration is a string),...

3 minutes read.

Python sorted() function

Python sorted() function The sorted() function returns a sorted list of the specified iterable object. Syntax sorted(iterable, *, key=None, reverse=False) Parameter iterable: It is a required parameter that represents the sequence to sort, list, dictionary, tuple etc. key:...

1 minute read.

Python Maurer Rose and Rhodonea Curves

In this tutorial, in Python we will make a Maurer Rose example and Rhodonea Curve example. Before we continue to see what precisely is maurer rose or a rhodonea bend...

10 minutes read.

Python divmod() function

Python divmod() function The divmod() function in Python returns a tuple containing the quotient  and the remainder when parameter ‘a’ (divident) is divided by parameter ‘b’ (divisor). Syntax: divmod(a, b) Parameter a: This parameter represents a number you...

1 minute read.

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

Socket Programming in Python

Socket Programming in Python In this tutorial, we will discuss network programming using Python programming language. We will explore all basic concept of network with Python script. Network Services in Python Python has...

9 minutes read.

GUI to Shut down, Restart and Logout from the PC using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

4 minutes read.

How to Make an App with Python

In this age of mobiles, rapid mobile application development has got a lot of traction. Moreover, app developers are high in demand because of the increase in digitization. Generally, Python is...

4 minutes read.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.

Python zip() Function

Python zip() Function The zip() function makes an iterator that aggregates elements from each of the iterables and returns an iterator of tuples. Syntax zip(*iterables) Parameter iterables: Iterator objects that will be joined together Return This function...

1 minute read.

Unit Testing in Python

The process of testing whether a particular unit is working properly or not is called “UNIT TESTING”. A unit test will check small components in your application. The first and...

7 minutes read.

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.

Installing Packages in Python

It's critical to understand that the term "package" here refers to a collection of software that must be installed (i.e. as a synonym for a distribution). It has nothing to...

6 minutes read.

Python String Lowercase

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Marshmallow

Python:  Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Palindrome In Python

What is Palindrome? A Palindrome can be defined as the number or a string that resides unchanged when it is reversed. Example: 14341 Output: Yes, this is a Palindrome number Example: RACECAR Output: Yes, this...

2 minutes read.

Find Last Occurrence of Substring using Python

Introduction When planning to work with strings, we may need to determine whether a substring is present. This issue is rather typical, and there have been numerous discussions about how to...

3 minutes read.