Python pywikibot – Get data from Wikipedia

In this post we will be creating a Python script that will retrieve data from Wikipedia, to do this we will be using the pywikibot library which interfaces with the MediaWiki API version 1.23 or higher.

To install the library run the following command.

pip install pywikibot

See the snippet of Python code below where we retrieve data from Wikipedia and print it to output. For this example we are retrieving the wiki results for the term ‘Microsoft’.

We start by creating a site object for the English Wikipedia and then provide our search term. The script then checks if the search exists and prints the content to output. We use a try and except block in the example below to cover any search queries that don’t exist.

import pywikibot

site = pywikibot.Site("en", "wikipedia")

page_title = "Microsoft"
page = pywikibot.Page(site, page_title)

if page.exists():
    page_text = page.text
    print("Page does not exist.")

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply