Wikipedia people on Raspberry Pi


, , , , , , , , , , ,

Monita, my Hispanic maid, sniffed my socks in disgust, then said quite unexpectedly, ‘I love those hunky wrestlers.’ It got me thinking. It got me coding Python on my Raspberry Pi. What if I could plough through a Wikipedia page on one of her favourite WWE superstars and show pictures of all the people linked to the article? She would positively swoon.

So I got cracking with Python Tools for Visual Studio, and within no time crafted the following delights:

from speech import Speech
from screen import Screen
import requests
from BeautifulSoup import BeautifulSoup

speech = Speech()
screen = Screen()


behindthename_errors = ["Error", "404 Not Found"]

# get webpage content
def get_soup(url):
    request = requests.get(url)
    return BeautifulSoup(request.text)

# get a full name from microphone
print("State your favourite wrestler")
full_name = speech.speech_to_text('/home/pi/PiAUISuite/VoiceCommand/')
# get wikipedia webpage
wikipedia_soup = get_soup(WIKIPEDIA + full_name.replace(" ", "_"))

# loop all wikipedia links
for link in wikipedia_soup.findAll('a', href=True):
    # check if link text exists
    text = link.text
    if not text:

    # get behindthename webpage
    behindthename_soup = get_soup(BEHINDTHENAME + text.partition(' ')[0])

    # check if forename valid
    if (behindthename_soup.find('h1') != None) and (behindthename_soup.find('h1').text in behindthename_errors):

    # display image of linked person
    screen.display_images(text, 1)

The first thing we do of note is to use Google Speech To Text service to convert our spoken words into text, through a microphone attached to the Raspberry Pi.

Next we get the Wikipedia webpage of the person we have requested, using the Beautiful Soup library for Python. Note that we feed the get_soup function with a url, appended with the formatted full name of the person.

Now we can loop through every hyperlink on the Wikipedia page. We use another website,, to check that the current link is actually of a person – if the website returns an error for the supplied forename then the link ain’t a person.

Finally, if all is well, we can render an image of the linked person in a browser. The image is sourced from a Google search.

Our Speech class handles the connection with Google Speech To Text service:

from subprocess import Popen, PIPE
class Speech(object):
    # converts speech to text
    def speech_to_text(self, filepath):
            # utilise PiAUISuite to turn speech into text
            text = Popen(['sudo', filepath], stdout=PIPE).communicate()[0]
            # tidy up text
            text = text.replace('"', '').strip()
            # debug

            return text
            print ("Error translating speech")

Our Screen class handles rendering an image of the linked person in a browser:

import requests
from BeautifulSoup import BeautifulSoup
import webbrowser

class Screen(object):

    # displays google images
    def display_images(self, search_term, amount):
            # check params
            if (amount < 1):

            #build url
            url = "{}&tbm=isch".format(search_term)
            #get webpage
            request = requests.get(url)
            soup = BeautifulSoup(request.text)    

            # get images
            images = []
            for img in soup.findAll('img'):

            # ensure enough images
            if amount > len(images):
                amount = len(images)

            # display each image in a browser
            for i in range(0, amount):
      [i], new=0 )
            print ("Error displaying images")

All that was left to do was to demo the program to Monita, and hope for a kiss on the cheek as reward. Here’s how it ran for American professional wrestler Randy Orton:


‘What do you think?’ I asked of her. She curled her lip and snorted. ‘It ain’t like the real thing, sweetie,’ she replied, tossing my socks into the washing basket as she left. What the fuck was she expecting?


Get every new post delivered to your Inbox.

Join 42 other followers