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.
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
Example #1: Extract the summary with
# 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.
Example #2: Searching title and suggestions
# getting suggestions import wikipedia result = wikipedia.search("python programming", results = 5) print(result) #Follow @code_snail
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])
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
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.
- Random Wikipedia Article Generator in Python
- Google Translator in Python
- Calendar in Python
- Secure Your Current Password using Python