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

Rail Fence Cipher Program in C and C++[Encryption & Decryption]

Here you will get rail fence cipher program in C and C++ for encryption and decryption. It is a kind of transposition cipher which is also known as zigzag cipher. Below is an example. Here Key = 3. For encryption we write the message diagonally in zigzag form in a matrix having total rows = key and total columns = message length. Then read the matrix row wise horizontally to get encrypted message. Rail Fence Cipher Program in C #include<stdio.h> #include<string.h> void encryptMsg(char msg[], int key){ int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0; char railMatrix[key][msgLen]; for(i = 0; i < key; ++i) for(j = 0; j < msgLen; ++j) railMatrix[i][j] = '\n'; for(i = 0; i < msgLen; ++i){ railMatrix[row][col++] = msg[i]; if(row == 0 || row == key-1) k= k * (-1); row = row + k; } printf("\nEncrypted Message: "); for(i = 0; i < key; ++i) f...

Data Encryption Standard (DES) Algorithm

Data Encryption Standard is a symmetric-key algorithm for the encrypting the data. It comes under block cipher algorithm which follows Feistel structure. Here is the block diagram of Data Encryption Standard. Fig1: DES Algorithm Block Diagram [Image Source: Cryptography and Network Security Principles and Practices 4 th Ed by William Stallings] Explanation for above diagram: Each character of plain text converted into binary format. Every time we take 64 bits from that and give as input to DES algorithm, then it processed through 16 rounds and then converted to cipher text. Initial Permutation: 64 bit plain text goes under initial permutation and then given to round 1. Since initial permutation step receiving 64 bits, it contains an 1×64 matrix which contains numbers from 1 to 64 but in shuffled order. After that, we arrange our original 64 bit text in the order mentioned in that matrix. [You can see the matrix in below code] After initial permutation, 64 bit text passed throug...

Accessibility Insights for the Web and Windows makes accessibility even easier

I recently stumbled upon https://accessibilityinsights.io . There's both a Chrome/ Edge extension and a Windows app, both designed to make it easier to find and fix accessibility issues in your websites and apps. The GitHub for the Accessibility Insights extension for the web is at https://github.com/Microsoft/accessibility-insights-web and they have three trains you can get on: Canary (released continuously) Insider (on feature completion) Production (after validation in Insider) It builds on top of the Deque Axe core engine with a really fresh UI. The "FastPass" found these issues with my podcast site in seconds - which kind of makes me feel bad, but at least I know what's wrong! However, the most impressive visualization in my opinion was the Tab Stop test! See below how it draws clear numbered line segments as you Tab from element. This is a brilliant way to understand exactly how someone without a mouse would move through your site. I can easily s...