Simple Guide to Wikipedia in Python

Wikipedia Module in Python | Wikipedia is a website all of use on a daily basis. Be it Cars, Countries or Computers, Wikipedia knows everything. Integrating Wikipedia and Python would, no doubt, be fun and useful for many projects. Doing it manually would have been a headache. Luckily for us, The Wikipedia Module makes our job simpler by doing most of the heavy-lifting work for us. Here is a simple guide on how to make full use of the Wikipedia Module. 

First things first, we need to download and install the Wikipedia Module. Here is the link: Download The Wikipedia Module

Now, of course, all projects using the Wikipedia Module must start with: 

import wikipedia

Everyone knows that reading the entire article may be a bit too much if you want some basic details. That is why Wikipedia offers a summary paragraph in the beginning of the article. How do you get that ? One line of code does it: 

print(wikipedia.summary("<Your Keyword Here>"))
 
This would print the summary paragraph of the keyword you entered. Just enter your keyword and execute the program to get the summary paragraph. 

There are however, some cases when a keyword may have more than one related article. Just take Python for example. Python could mean the programming language or the snake. This is why we have the search function. The search function takes a keyword and returns a list of all related articles. 

print(wikipedia.search('Python'))

The output for this would look something like this: 

['Python', 'Python (programming language)', 'Monty Python', 'Reticulated python', 'PYTHON', 'Ball python', 'History of Python', 'Burmese python', 'Python (missile)', 'African rock python']
>>> 

This list contains the names of all the articles that are related to the word Python. Just select the one you wanted and use it in the summary function or any other function. 

Wikipedia also gives us a feature to get the title, URL, article and the links from any given page. To do that, just use the page function, assign it to a variable for easy usage and use the functions as required, like this: 

python = wikipedia.page('Python (Programming Language)') #Get the Page

print(python.title) #Get the Page Title

print(python.url) #Get the Page URL

print(python.content) #Get the Page Content

print(python.links) #Get all the Page Links

But all of this is in English. Not everyone wants their text to be in English. What if I want to get a summary in French. the Wikipedia Module offers a function for that too. All you need is this one line of code: 

wikipedia.set_lang('fr') 

Since we need our summary to be in French, we use the code fr. Just replace the code with the ISO code of any language you want it in. 

So, here is a overview of the Wikipedia Module. If you found this post useful, follow ThePygrammer to be updated every time a new post comes out. Feel free to use the comment section to share your queries. 

You might also find these posts interesting: 

Comments