×

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

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

Data Structures and Algorithms Using Python | Part 1

Data Structures: Data Structure is defined as a way to organize and store the data so that we can access the data and work more efficiently. Data structures also describe the...

18 minutes read.

Convert uppercase to lowercase in Python

Python String lower() By using the built-in string lower() method, all the uppercase characters can be converted into lowercase characters in a string, The lowercased string from the given string is returned...

1 minute read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.

Why learn Python?

All things considered, learning python would be extraordinary, it'll acquaint you with the universe of dynamic programming dialects, if you're somebody who has had a semester of involvement with C. Python...

4 minutes read.

Python String

Python String Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written...

31 minutes read.

Introduction to Scratch programming

Scratch programming is generally designed for children who may create digital stories, games, and animations using the computer language Scratch, which has the largest kid-focused community in the world. Scratch...

3 minutes read.

Best books for NLP with Python

NLP NLP – Natural language processing Computers were created to make the life of mankind easier. But computers cannot understand human language. Instead, they interact with numbers to perform the tasks allotted...

12 minutes read.

Python for loop

 Python for loop A for loop in Python executes a block of code for a specified number of times, based on a given sequence. The for loop in Python is different than any other...

3 minutes read.

Simple GUI calculator using PyQt5 in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

4 minutes read.

How to Add Elements in List in Python?

How to Add Elements in List in Python We all are aware of the wide spectrum of applications where the data structures of Python are used. Be it lists, tuples, or...

4 minutes read.

How to Open a file in python with Path

In this tutorial, we will see rather than the traditional way of opening a file by locating or navigating it from our desktops; we will learn how to open the...

2 minutes read.

How to Install Python

Python Installation Guide Step by Step Installing Python is quite simple and easy. In this tutorial, we will show stepwise procedure to install Python and set up the Python environment in different...

2 minutes read.

Python Bitwise Operators

When used with variables and objects in an expression, operators are tokens that carry out calculations. The variables and items to which the computation is applied are known as operands....

5 minutes read.

Python String rstrip() method

Python String rstrip() method The string.rstrip() method in Python returns a copy of the string with trailing characters removed. Syntax string.rstrip([chars]) Parameter chars:  This argument represents a string specifying the set of characters to be...

1 minute read.

Python String zfill() method

Python String zfill() method The string.zfill() method in Python returns a copy of the string while adding the zeros (0) at the beginning of the string, until it reaches the specified...

1 minute read.

Python Identifiers

Identifiers in Python User-defined names are identifiers in Python that are used to name variables, functions, classes, modules, and other things. You can create Python identifiers using these rules: As an identifier...

4 minutes read.

Python Trigonometric Functions

Before jumping right into the functions and how to use them we need to know what do trigonometric functions mean and how many functions does python has. What are Trigonometric functions? Trigonometric...

3 minutes read.

XXhash Python

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...

6 minutes read.