How to use Wikipedia Module in Python

How to use Wikipedia Module in Python

In this tutorial, we learn how to use wikipedia module in Python and where we need to use will be explained with examples.

Wikipedia it is a platform in internet which has huge amount of data. It is an Open-source, can be managed by volunteer editors using wikiediting system. It is a multi-lingual encyclopedia.

To use Wikipedia module in Python we need to install the module first.

Installation

This wikipedia module will help you to search the result from wikipedia with just one line of code. This module will bundle-up the official Wikipedia API. The primary step, we install the wikipedia module using the pip command.

pip install wikipedia

From the command given we can install the module now, we need to import it using command

import wikipedia

By importing the wikipedia the data can be uprooted from the wikipedia

Using wikipedia module

This wikipedia module consists of various built-in methods in it which helps getting desired data or information. this module allow us to search a query supplied as an argument using search() method. In the search() method it returns all the list of articles which contains the query that was searched for.

Example
import wikipedia
print(wikipedia.search("Program")

Output

['Computer Program', 'Program', 'Programmer', 'Television program', 'software', 'radio program', 'program counter', 'program evaluation']
Explaination

From the output, we can see that the method returned the title and the related search are been wrtten. We can also take only limited number of related searches by using a value for result parameter.

This result parameter will be bounded by a distinct number. Which helps us getting only limited number of values.

Example
import wikipedia
print(wikipedia.search("Program", result=3)

Output

['Computer Program', 'Program', 'Programmer']
Explaination

The above code will print only limited as result is 3 so, it gives only 3 related search words as we mentioned.

Using suggestion

This suggestion name itself we can understand, in this method it returns the suggested wikipedia title for the query or None, if doesn’t found anything in result.

Example
import wikipedia
print(wikipedia.suggest("conoyre")

Output

None

Explaination

In the above example code, we wanted to search for computer but because of wrong spelling we didn’t. The suggestion() method resulted None because the method didn’t find any query that given in code.

Summary of the article

This module also has summary() method, which results in giving summary of the article or topic. In this method it has two arguments title and sentences, and in result it returns the summary in the string format.

Example
import wikipedia
print(wikipedia.summary("Global Warming", sentences = 2)

Output

Global warming is the usually rapid increase in Earth's average surface temperature over the past century primarily due to the greenhouse gases released as people burn fossil fuels.
Note:

Also this method raises a ‘disambiguation error’ if the required page does not exist.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top