The books instructs you to visit this link:
But once you visit it you will encounter this error:
So, using my investigation skills, I looked up what the error meant. It’s exactly what it says in the error message. Error 401 is basically letting you know that you do not have a valid API key/id to be able to access their API for your program. When I first came across this information, to be honest, I just completely skipped this project and thought it just wasn’t going to work. Yesterday, while writing this blog post, I got an epiphany about how I was going to make this work. I went back to the site and read the error again and decided to follow the link to create an account on the openweathermap website and generate an API key. I’m glad that I took it upon myself to go back and research because the process is very similar to my last project of me hacking Twitter’s API. After creating an account and obtaining a key I did some research on YouTube about how to insert the key appropriately in the code to get weather results. I came across the YouTuber “siddhesh kadam” and in this video:
He explains how to go about getting an api key, getting weather results in your browser, and getting weather results in your program. You can follow along with his code example if necessary, but I stuck with the code in LPWRP. The code in the book is a little outdated so I had to make some adjustments for it to work. I also got some insight from my twitter friends @lord_death216, @martinblore, and @mrgrueny for figuring out some issues I was having.
The first part of this program shows how python can pull information from the internet using the url-lib.request module:
When ran you should get a similar output to the one shown below:
The data is a bit hard to read because it is in string format but the JSON module can load this information and return a dictionary that contains the various parts of it making it a little easier to read. An example of this is shown below:
I can now get any information that I want from forecast_dict data structure. For the last part of this program the book produces this code for us to be able to print out a seven-day weather forecast:
Unfortunately, I believe it was a bit outdated. When ran the stack trace reviled these errors:
A KeyError just means that specific key does not exist. In this case it would be ‘city’ which was not in the dictionary for this program. After discussing with a few of my twitter friends, @lord_death216, @martinblore, @mrgrueny, they helped me find a solution to this issue. I changed ‘city’ to ‘sys’ and tried running the code and this was output:
I am currently looking for the fix this list key error. I’ve found that when I practice my research skills when I find an error help with learning more about the language. After more revisions in the code, with the help of @martinblore, we got a more simplified version of the code to give us similar results to the first two programs but with more intelligible results.
This version of the program prints a description of the weather, the temperature in kelvin, the pressure, humidity, and wind speed. You can type any city into the program and it will produce the same results. This project is a really great starter project for those looking to dive into using JSON, APIs, and python and to add to your portfolio.