×

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

Check if the directory exists in Python

To prevent any error, while loading or editing a file, we first have to check that the directory or the file exists or not. This is also done to prevent...

5 minutes read.

Decision Tree in Python

Decision Tree is one of the most essential algorithms in the area of machine learning for classification and regression. But let us first talk about the lifespan of every machine learning...

12 minutes read.

Python frozenset()

Python frozenset() class The frozenset() class in Python returns a new frozenset object, optionally with elements taken from iterable. Syntax class frozenset([iterable]) Parameter iterable: This parameter represents an iterable object, like list, set, tuple etc. Return This class returns an unchangeable...

1 minute read.

List Assignment Index out of Range in Python

As we know, a List is one of the four unique data structures available in Python; In this tutorial, we will deep dive into understanding iterating through a list and...

3 minutes read.

How to declare array in python

How to declare array in python? Arrays are a great way of storing our numeric values into a variable in continuous locations. For declaring an array in C language, we used...

4 minutes read.

Python Parse Text File

We will learn different ways of read text records in Python. TL;DR The accompanying tells the best way to read all texts from the readme.txt document into a string: with open('readme.txt') as f: lines...

5 minutes read.

Executing Shell Commands in Python

This tutorial aims to make us understand what a Shell is, what is the importance of a Shell, what are Shell commands in Python, and how can we execute the...

3 minutes read.

STL in Python

Python has many libraries that are very useful and simplify many complex codes. In the same way, STL is also one of the python libraries used for reading and writing...

3 minutes read.

Features of Python

Python is a powerful, easy to learn, popular programming language. It has effective data structures and its elegant syntax makes it user-friendly language. Below are the key features of Python: 1. Python...

4 minutes read.

Python List Size

Introduction The list data type in Python is an ordered, flexible collection. A list may also contain duplicate entries. To get the size of any object, use the len() function in...

6 minutes read.

Python Hash Table

An Introduction to Hashing A technique which used to identify a particular object uniquely from a set of similar objects is known as Hashing. Some real-life examples of hashing implementations include: A...

9 minutes read.

Python max() function

Python max() function The max() function in Python returns the largest item in an iterable or the largest of two or more arguments. Syntax max(iterable, *[, key, default])               or max(arg1, arg2, *args[, key])   Parameter arg1, arg2, *args: This...

1 minute read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

Python Letter to Number

Python Letter to Number In this tutorial, we will convert the given letters into numbers using a Python. We will convert the given letter into the letter value as defined in...

3 minutes read.

Anaconda in Python 3

What is Anaconda in Python 3? Anaconda is an open-source package combining Python and R programming languages used for data analytics and processing data. It consists of a huge number of...

4 minutes read.

Python Set symmetric_difference_update() method

Python Set symmetric_difference_update() method The set.symmetric_difference_update() method in Python updates the original set by removing items that are present in both sets and inserting the other items of the set. Syntax set.symmetric_difference_update(set1) Parameter set- This parameter represents the first...

2 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

The Calendar Module of Python

The calendar module provides calendar features, including printing functions for a specific month or year. Default is the first day of the week on Monday and the last day on Sunday...

3 minutes read.

Python for Data Analysis

Data analysis uses various techniques to read, illustrate, manipulate and evaluate a particular data. You can have access to the data and keep the data updated regularly. You can append...

4 minutes read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.