Python Tutorial

Introduction Python Features Python Applications System requirements for Python Python Installation Python Basics Python Variables Python Data Types Python IDE Python Keywords Python Operators Python Comments Python Pass Statement

Python Conditional Statements

Python if Statement Python elif Statement Python If-else statement Python Switch Case

Python Loops

Python for loop Python while loop Python Break Statement Python Continue Statement Python Goto Statement

Python Arrays

Python Array Python Matrix

Python Strings

Python Strings Python Regex

Python Built-in Data Structure

Python Lists Python Tuples Python Lists vs Tuples Python Dictionary Python Sets

Python Functions

Python Function Python min() function Python max() function Python User-define Functions Python Built-in Functions Anonymous/Lambda Function in Python

Python File Handling

Python File Handling Python Read CSV Python Write CSV Python Read Excel Python Write Excel Python Read Text File Python Write Text File Read JSON File in Python

Python Exception Handling

Python Exception Handling Python Errors and exceptions Python Assert

Python OOPs Concept

OOPs Concepts in Python Classes & Objects in Python Inheritance in Python Polymorphism in Python Python Encapsulation Python Constructor Static Variables in Python Abstraction in Python

Python Iterators

Iterators in Python Yield Statement In Python

Python Generators

Python Generator

Python Decorators

Python Decorator

Python Functions and Methods

Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods

Python Modules

Python Modules Python Datetime Module Python Calendar Module  

Python MySQL

Python MySQL Python MySQL Update Operation Python MySQL Delete Operation

Python MongoDB

Python MongoDB

Python Data Structure Implementation

Python Stack Python Queue Python Hash Table Python Graph

Python Advance Topics

Speech Recognition in Python Face Recognition in Python Python Rest API Python Command Line Arguments Python JSON Python Virtual Environment Type Casting in Python Collections in python Python Enumerate Python Debugger Python DefaultDict

Misc

Python PPTX Python Pickle Python Seaborn Python Coroutine Python EOL Python Infinity Python math.cos and math.acos function Python Project Ideas Based On Django Reverse a String in Python Reverse a Number in Python Python Word Tokenizer Python Trigonometric Functions Python try catch exception GUI Calculator in Python Implementing geometric shapes into the game in python Installing Packages in Python Python Try Except Python Sending Email Socket Programming in Python Python CGI Programming Python Data Structures Python abstract class Python Compiler Python K-Means Clustering List Comprehension in Python3 NSE Tools In Python Operator Module In Python Palindrome In Python Permutations in Python Pillow Python introduction and setup Python Functionalities of Pillow Module Python Argmin Python whois Python JSON Schema Python lock Return Statement In Python Reverse a sentence In Python tell() function in Python Why learn Python? Write Dictionary to CSV in Python Write a String in Python Binary Search Visualization using Pygame in Python Latest Project Ideas using Python 2022 Closest Pair of Points in Python ComboBox in Python Python vs R Python Ternary Operators Self in Python Python vs Java Python Modulo Python Packages Python Syntax Python Uses Python Logical Operators Python Multiprocessing Python History Difference between Input() and raw_input() functions in Python Conditional Statements in python Confusion Matrix Visualization Python Python Algorithms Python Modules List Difference between Python 2 and Python 3 Is Python Case Sensitive Method Overloading in Python Python Arithmetic Operators Design patterns in python Assignment Operators in Python Is Python Object Oriented Programming language Division in Python Python exit commands Continue And Pass Statements In Python Colors In Python Convert String Into Int In Python Convert String To Binary In Python Convert Uppercase To Lowercase In Python Convert XML To JSON In Python Converting Set To List In Python Covariance In Python CSV Module In Python Decision Tree In Python Difference Between Yield And Return In Python Dynamic Typing In Python Abstract design pattern in python Builder design pattern in python Prototype design pattern in Python Creational design patterns in Python

How to

How to convert integer to float in Python How to reverse a string in Python How to take input in Python How to install Python in Windows How to install Python in Ubuntu How to install PIP in Python How to call a function in Python How to download Python How to comment multiple lines in Python How to create a file in Python How to create a list in Python How to declare array in Python How to clear screen in Python How to convert string to list in Python How to take multiple inputs in Python How to write a program in Python How to compare two strings in Python How to create a dictionary in Python How to create an array in Python How to update Python How to compare two lists in Python How to concatenate two strings in Python How to print pattern in Python How to check data type in python How to slice a list in python How to implement classifiers in Python How To Print Colored Text in Python How to develop a game in python How to print in same line in python How to create a class in python How to find square root in python How to import numy in python How to import pandas in python How to uninstall python How to upgrade PIP in python How to append a string in python How to open a file in python

Sorting

Python Sort List Sort Dictionary in Python Python sort() function Python Bubble Sort

Programs

Factorial Program in Python Prime Number Program in Python Fibonacci Series Program in Python Leap Year Program in Python Palindrome Program in Python Check Palindrome In Python Calculator Program in Python Armstrong Number Program in Python Python Program to add two numbers Anagram Program in Python Even Odd Program in Python GCD Program in Python Python Exit Program Python Program to check Leap Year Operator Overloading in Python Pointers in Python Python Not Equal Operator Raise Exception in Python Salary of Python Developers in India What is a Script in Python Singleton design pattern in python

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.



ADVERTISEMENT
ADVERTISEMENT