Skip to main content

Wikipedia API Python Tutorial

In this tutorial I’ll show you how we can implement Wikipedia API in Python to fetch information from a Wikipedia article. Let’s see how to do it.

First we have to install Wikipedia. To install it, open your command prompt or terminal and type this command.

pip install wikipedia

That’s all we have to do. Now we can fetch the data from Wikipedia very easily.

Wikipedia API Python Tutorial

To Get the Summary of an Article

import wikipedia
print(wikipedia.summary("google"))

It will fetch the summary of google from wikipedia and print it on the screen.

To Get a Given Number of Sentences From the Summary of an Article

import wikipedia
print(wikipedia.summary("google", sentences=1))

Output:

Google LLC is an American multinational technology company that specializes in Internet-related services and products, which include online advertising technologies, search engine, cloud computing, software, and hardware.

Same way you can pass any number as a parameter to get the number of sentences you want.

To Change the Language of the Article

import wikipedia
wikipedia.set_lang("fr")
print(wikipedia.summary("google", sentences=1))

Output:

Google (prononcé [ˈguːgəl]) est une entreprise américaine de services technologiques fondée en 1998 dans la Silicon Valley, en Californie, par Larry Page et Sergueï Brin, créateurs du moteur de recherche Google.

Here fr stands for French. You can use any other code instead of fr to get the information in other language. But make sure that the Wikipedia should have that article in the language you want.

To see the code of other languages open this link https://www.loc.gov/standards/iso639-2/php/code_list.php

Search to Get the Titles of the Articles

import wikipedia
print(wikipedia.search("google"))

Output:

[‘Google’, ‘Google+’, ‘Google Maps’, ‘Google Search’, ‘Google Translate’, ‘Google Chrome’, ‘.google’, ‘Google Earth’, ‘Gmail’, ‘Google Scholar’]

The method search() will return a list which consist of all the article’s titles that we can open.

To Get the URL of the Article

import wikipedia
page = wikipedia.page("google")
print(page.url)

Output:

https://en.wikipedia.org/wiki/Google

First wikipedia.page() will store all the relevant information in variable page. Then we can use the url property to get the link of the page.

To Get the Title of the Article

import wikipedia
page = wikipedia.page("google")
print(page.title)

Output:

Google

To Get Complete Article

import wikipedia
page = wikipedia.page("google")
print(page.content)

Complete article from starting to end will be printed on the screen.

To Get the Images Included in Article 

import wikipedia
page = wikipedia.page("google")
print(page.images[0])

Output:

https://upload.wikimedia.org/wikipedia/commons/1/1d/20_colleges_with_the_most_alumni_at_Google.png

So it will return us the URL of the particular image present at index 0. To fetch another image use 1, 2, 3, etc, according to images present in the article.

But if you want image to be downloaded into your local directory instead of printing the result then we can use urllib. Here’s the program which will help you to download an image from the link.

import urllib.request
import wikipedia
page = wikipedia.page("Google")
image_link = page.images[0]
urllib.request.urlretrieve(image_link , "local-filename.jpg")

The image present at index 0 will  be saved as local-filename.jpg into the same directory where your program is saved. The above program will work for python 3.x, if you’re using Python 2.x then please see the program below.

import urllib
import wikipedia
page = wikipedia.page("Google")
image_link = page.images[0]
urllib.urlretrieve(image_link , "local-filename.jpg")

That’s all for this article, for more information please visit https://pypi.org/project/wikipedia/

If you’ve any problem or suggestion related to wikipedia python api then please comment below.

The post Wikipedia API Python Tutorial appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2018/05/wikipedia-api-python-tutorial.html

Comments

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan

15 Web Design Trends to Watch in 2018

The modern world is full of extraordinary things that influence our imagination and mood. Our soul needs a perfect atmosphere and impressive spots. To apply such things in practice, we have submitted the list of the web trends that deserve your attention. Robert frost design analysis will meet all your wishes and expectations. Image Source Web Design Trends to Watch in 2018 1. More Organic Shapes Until this year, web design, as well as mobile design, were based on the right-angled and sharp-edged shapes. However, it seems that this year will bring some significant changes in the field of web design. The recent trends will offer the absolute rounded corners. In addition, the web design of 2018 will make the real things look like the cartoonish ones. 2.   Bold Minimalism Although some of you may think that this web design trend will not attract the Internet users. Indeed, the notion of minimalism is often associated with boredom and dullness. However, in this case, bold minimalis

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and