How to Create Your Own Weather Forecast Program Using Python

In this tutorial we’ll explore how to access the Wunderground API (application programming interface) using Python. What we create will be very simple but it will give you a good idea of what you can do with Python and with APIs.

Wunderground (and most APIs, such as those provided by Twitter, Facebook, and Reddit) require you to create a free API key. The API key is a code passed by programs calling the API to identify the calling program and its developer. Keys are used to track and control how the API is being used, and to prevent malicious use or abuse of the API.

To create your key, create a free account here. Click the link in the confirmation email that you’ll receive, and then sign into your account. Click “Explore My Options,” and then “Purchase Key” at the bottom of the next screen (don’t worry, you won’t be spending any money). Complete the questions on the next page (put your real name and contact email; set the project name to something like “API Test” and the website to ist.psu.edu or whatever you’d like). Select “Website”, “No”, and “No” for the three radio buttons. Enter your country and a brief description (“Learning about the Wunderground API”), and then check both boxes and click “Purchase Key.” On the next page, you’ll see your key (it will be 16 hexadecimal characters, such as “4f73eacea9c60244”). Copy and paste this into somewhere handy, because you’ll need it soon.

Wunderground’s API provides answers to a variety of questions that we can ask it. Let’s say that we were interested in learning the current weather conditions in State College. In our Web browser, we can simply construct a special URL, containing our API key and the city and state for University Park, and we’ll get a plain-text set of results back. Here’s how to build the URL (note that you must replace [your API key] with, you guessed it, your API key):

http://api.wunderground.com/api/[your API key]/conditions/q/16802.json

As you can see from the URL, here we’re looking at “conditions” data, which refers to the current weather. Looking at the results, you can probably see fields like zip, latitude, longitude elevation, weather, temp_f, wind_string, and feelslike_f that look interesting. What we’d like to do in this tutorial is: 1) create a Python script that returns only the information we care about; and 2) accesses the forecast data, rather the current condition data. So, let’s do that.

You may need to install Python and IDLE on your computer; you can do that here. If you’re an IST student, you can log in to https://svg.up.ist.psu.edu. Open the Start menu and type “Python”, and then launch “IDLE (Python GUI)”.

In IDLE, select File -> New File (or New Window). In that window, type the following (again, you need to replace [your API key]):

Then select Run -> Run Module. If everything worked, you should be in a loop where the script asks for your ZIP code and then spits out the current weather conditions. So far, so good. We want to access forecast data, though, and not just current condition data.

Create a new script and type and run the following (again, you need to replace [your API key]):

You can explore many more features of the Wunderground API here. If you want to learn more about Python, I think the best place to start is here.