Wikipedia in Python 🌐
Wikipedia in Python: Wikipedia is an awesome place to complete assignments right. And with Wikipedia being one of the largest and most popular sources for information on the Internet.
Wikipedia is a multilingual online encyclopedia created and maintained as an open collaboration project by a community of volunteer editors using a wiki-based editing system.
Today we bring wikipedia in our python program. Let’s see.
Installation
In order to extract data from Wikipedia, we must first install the Python Wikipedia library, which wraps the official Wikipedia API. This can be done by entering the command below in your command prompt or terminal:
pip install wikipedia
Code
Example #1: Extract the summary with summary
method.
# Extract the summary with summary method
import wikipedia
result = wikipedia.summary("India", sentences = 2)
print(result)
# Follow @code_snail
Here, sentences = 2 refers to numbers of line.
Output:
Example #2: Searching title and suggestions
# getting suggestions
import wikipedia
result = wikipedia.search("python programming", results = 5)
print(result)
#Follow @code_snail
Output:
Example #3: Getting Full Wikipedia Page Data
The page() method is used to get the contents, categories, coordinates, images, links and other metadata of a Wikipedia page.
import wikipedia
# wikipedia page object is created
page_object = wikipedia.page("facebook")
# printing title
print(page_object.original_title)
# printing 5 links on that page object
print(page_object.links[0:5])
Output:
Example #4: Wikipedia page in Other langauges
The language can be changed to your native language if the page exists in your native language. Set_lang()
method is used for the same.
import wikipedia
# in greek language
wikipedia.set_lang("hi")
result=wikipedia.summary("India")
# printing the summary
print(result)
#Follow @code_snail
Output:
More about module: https://pypi.org/project/wikipedia/
I hope you like this wikipedia in python code. Must try and share it with your friends. For more codes be connected with us and press the bell icon.
Also see,