Hello fellow learner! Today in this tutorial, we are going to learn about a new module named Wikipedia which can be used to get information about anything required.
So let’s get started.
Introduction to the Wikipedia Module in Python
Wikipedia module can be used to fetch a bunch of information from the Wikipedia website we are all familiar with.
We will be starting off by importing the
wikipedia module into our program. If the importing command gives an error. Make sure you install the module using the
Getting Data from Wikipedia Module
Now let us learn how to actually implement the wikipedia module in Python.
1. Getting random page names
Choosing good titles to search for can be a tough task at time. One can get random titles using the
The method can take the no of pages as a parameter if we need more than one random title. The function returns a list of titles.
The code for the same is shown below.
import wikipedia print(wikipedia.random(pages=5))
The output of the function is displayed below.
['Bharathi Kannamma', 'Sancergues', 'Live in Gdańsk', 'Allery Sandy', 'Ronald (disambiguation)']
2. Getting the summary
summary method can be used to get the summary of any tittle. The same can be done by using the code below.
summary method takes a string as a parameter which specifies the title to search for. It returns a number of sentences for the title mentioned.
We can also add the no of sentences we need as a parameter to limit the data stored. The code for the same is shown below.
s1 = wikipedia.summary('Frank Johnson (musician)',sentences=50) print(s1)
The output of the code is shown below.
Frank Johnson (c. 1789 – 1871) was an American popular fiddle player and brass band leader based in North Carolina, near Wilmington, United States, for most of the nineteenth century. Although largely forgotten by history books and often confused with composer Francis "Frank" Johnson, he helped define the sound of African-American fiddle and brass-band music in the mid-19th century. == Personal life == Johnson was born into slavery circa 1789, in North Carolina, and became a free man sometime before 1830. He showed a talent for music early on and established himself as a popular fiddle player for dances. Using money he earned from performances, he bought the freedom of himself, his wife and his children. A contemporary account of Johnson while performing at a "pic nic" describes him: "To say that he is handsome would not be strictly true, and still, when he is living so full of music that his features follow the changes of his tune, it is fair to say he looks very 'becoming'."He was buried in Pine Forest Cemetery, Wilmington, after a well-attended funeral: "the largest, we think, that has ever occurred in this city, it being estimated that there were at least two thousand persons in the procession, including the colored fire companies in uniform, with standards draped in mourning, the colored Masonic fraternity in regalia, etc., the whole preceded by a brass band." == Career == Johnson assembled his freed sons and various nephews into an eponymous brass band by 1830. The band consisted of about 15 members. Johnson himself played many instruments, but was known for his mastery of the fiddle, clarinet, and cornet. The Frank Johnson Band was popular with white planters and often played for state fairs, picnics, cotillions, college commencement balls (e.g., at Chapel Hill, North Carolina), and political rallies (but only for Democrats).
3. Get the whole Wikipedia page
To get a whole page from Wikipedia, we make use of the
page function which takes the title of the page as a parameter.
The function returns a page object for the title mentioned. We can further extract data from the page object created. The code for the same is shown below. But printing the page object created won’t result in anything informative.
To get the data from the page object, we are required to mention the exact information we need from the page.
Check out the code below.
page_obj = wikipedia.page('Yarwil') print(page_obj) print("TITLE OF THE PAGE:\n",page_obj.original_title) print("\n\n") print("CATEGORIES OF THE PAGE CHOOSEN:\n",page_obj.categories) print("\n\n") print("CONTENTS OF THE PAGE INCLUDE:\n",page_obj.content)
The output of the code mentioned above is shown below.
<WikipediaPage 'Yarwil'> TITLE OF THE PAGE: Yarwil CATEGORIES OF THE PAGE CHOOSEN: ['All stub articles', 'Articles with short description', 'Companies based in Bærum', 'Norwegian company stubs', 'Short description matches Wikidata', 'Technology companies of Norway', 'Use dmy dates from January 2014'] CONTENTS OF THE PAGE INCLUDE: Yarwil AS is a joint venture between Yara International and Wilhelmsen Maritime Services. The Norwegian registered company provides systems for reduction of NOx emissions from ship engines. The technology is based on the Selective Catalytic Reduction (SCR) method using Urea as a reactant. This method can reduce NOx emissions from ships by as much as 95%. The company was established as a reaction to the increased focus by the global community on emissions to air from the maritime industry. New IMO regulations, MEPC 58, are in place, which demand a reduction in NOx emissions from ships globally of 20% by 2011 and 80% by 2016. There are several different technologies available for the reduction of NOx, however the Selective Catalytic Reduction method is the only known technology that can reach the 2016 target of 80%. Yarwil was registered on 22 August 2007 and has its headquarters at Lysaker just outside Oslo in Norway.On 21 October 2013 a press release was issued by Yara International stating they had acquired full ownership of Yarwil and that the company would become part of their NOxCare initiative as of 1 January 2014. == References == == External links == Acticle about Yarwil in Emissions Worldview Article about Yarwil by Lloyd's List Article on NOx reduction by Bellona NOxCare.com
4. Getting data in different language
To get information in a different language, we will be using
set_lang function and mention the language as a parameter.
The function converts the data into the language mentioned. The code for the same is shown below. In the code below we will be getting the info in *French language.
The output turns out to be something like what’s shown below.
Mickey Mouse [mikɛ maus] (en anglais : [ˈmɪki maʊs] ) est un personnage de fiction américain appartenant à l'univers Disney, apparaissant principalement dans des dessins animés, dans des bandes dessinées et des jeux vidéo. Véritable ambassadeur de la Walt Disney Company, il est présent dans la plupart des secteurs d'activité de la société, que ce soit l'animation, la télévision, les parcs d'attractions ou les produits de consommation. Mickey est utilisé comme un vecteur de communication et ses qualités doivent respecter la morale prônée par « Disney », que ce soit par Walt ou par l'entreprise elle-même. Mickey Mouse est connu et reconnu dans le monde entier, sa célèbre silhouette formée de trois cercles étant devenue indissociable de la marque Disney. Mickey a été créé en 1928, après que Walt Disney eut dû laisser son premier personnage créé avec Ub Iwerks, Oswald le lapin chanceux, à son producteur.
So, today in this tutorial we learned about a new library named wikipedia to gather information about a certain topic.
Hope you learned something new! Thank you for reading!