Practical Coding in Python

Learn to write and validate your own code

Darren Kessner, PhD

(revised September 1, 2025)

Previous: Hello emoji

Hello requests

#
# hello_requests.py
#


# Installation: 'requests' in requirements.txt


import requests  


# The `requests` library allows you to send HTTP requests, for
# accessing web pages or web APIs.
# https://requests.readthedocs.io

# Weather API documentation 
# https://www.weather.gov/documentation/services-web-api

# National Weather Service (NWS) 
# https://www.weather.gov/

# NOAA 
# https://www.noaa.gov/


def main():

    # https://api.weather.gov/points/{latitude},{longitude}
    url = "https://api.weather.gov/points/34.0699142,-118.3294098"

    # Use the `get()` function to retrieve a webpage (GET request).

    response = requests.get(url)

    # Status code 200 is success.

    print("status_code:", response.status_code)
    print()

    if response.status_code != 200:
        print("Error.")
        return

    # The response from a webpage will generatlly be HTML.  The
    # response from a web API will usually be in JSON format, which
    # you can think of as a bunch of nested dictionaries.

    result_json = response.json()
    properties = result_json['properties']

    city = properties['relativeLocation']['properties']['city']
    forecast_url = properties['forecast']

    print("city:", city)
    print("forecast_url:", forecast_url)
    print()

    # Accessing the actual forecast requires a second GET request.

    response = requests.get(forecast_url)

    properties = response.json()['properties']
    periods = properties['periods']

    for period in periods:
        print(period['name'])
        print(period['detailedForecast'])
        print()


if __name__ == '__main__':
    main()

Output:

status_code: 200

city: West Hollywood
forecast_url: https://api.weather.gov/gridpoints/LOX/152,46/forecast

Today
A chance of rain. Mostly cloudy, with a high near 67. Southeast 
wind around 5 mph. Chance of precipitation is 50%. New rainfall 
amounts less than a tenth of an inch possible.

Tonight
Rain. Cloudy, with a low around 58. East southeast wind around 5 
mph. Chance of precipitation is 80%. New rainfall amounts between a 
tenth and quarter of an inch possible.

Saturday
Rain. Cloudy, with a high near 63. Southeast wind 5 to 10 mph. 
Chance of precipitation is 90%. New rainfall amounts between a half 
and three quarters of an inch possible.

Saturday Night
Rain before 10pm, then a chance of rain showers between 10pm and 
4am, then a chance of rain. Cloudy, with a low around 55. Southeast 
wind 5 to 10 mph. Chance of precipitation is 80%. New rainfall 
amounts between a half and three quarters of an inch possible.

Sunday
Rain likely. Cloudy, with a high near 62. South wind 5 to 10 mph. 
Chance of precipitation is 70%. New rainfall amounts between a 
quarter and half of an inch possible.

Sunday Night
Rain likely. Mostly cloudy, with a low around 52. Chance of 
precipitation is 70%.

Monday
A chance of rain. Mostly cloudy, with a high near 62.

Monday Night
A chance of rain. Mostly cloudy, with a low around 50.

Tuesday
A chance of rain. Partly sunny, with a high near 62.

Tuesday Night
A slight chance of rain before 10pm. Partly cloudy, with a low 
around 47.

Wednesday
A slight chance of rain after 4pm. Partly sunny, with a high near 
63.

Wednesday Night
A slight chance of rain. Mostly cloudy, with a low around 46.

Thursday
A slight chance of rain before 4pm. Mostly sunny, with a high near 
62.

Thursday Night
Partly cloudy, with a low around 44.

Next: