We can use python to develop a program to capture any city’s weather forecast from web site under our Raspberry Pi computer. I list our program and screen capture result in the following for your reference.

Program Source Code under Python ver 3 as below:

import urllib.request,json

city = input(“Enter City: “)

def getForecast(city) :
    #url = “”
    #url = “”
    #url = “”
    url = “”
    url = url + city + “&lang=zh_cn&APPID=42443ecbdcad3d01842205e3745895cd”
    req = urllib.request.Request(url)
    return json.loads(“UTF-8″))

forecast = getForecast(city)

print(“Forecast for “, city, forecast[‘city’][‘country’])

for day in forecast[‘list’]:
    print(“Day : “, day_num)
    print(“Cloud Cover : “, day[‘clouds’])
    print(“Temp Min : “, round(day[‘temp’][‘min’]-273.15, 1), “degrees C”)
    print(“Temp Max : “, round(day[‘temp’][‘max’]-273.15, 1), “degrees C”)
    print(“Humidity : “, day[‘humidity’], “%”)
    print(“Wind Speed : “, day[‘speed’], “m/s”)
    day_num = day_num+1

Screen Dump Result as below:


Further Development:

This function can be further developed to save weather forecast data to database, and then display in a chart format on screen. I may do it if I have time in future.

Leave a Reply

Your email address will not be published.

Solve : *
5 + 7 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>