''' Purpose: provide weather forecast by accessing US Weather Service web service Usage: user provides a zipcode Output: municipality and state combo along with its current forecast ''' # get url access capabilities from cs 1112 module import url # define weather.gov base query WEATHER_GOV_QUERY = 'https://forecast.weather.gov/zipcity.php?inputstring=' # forecast delimiters FRONT_DELIMITER = '
' # this precedes forecast REAR_DELIMITER = '
' # this follows forecast # delimiter lengths LENGTH_FRONT_DELIMITER = len( FRONT_DELIMITER ) LENGTH_REAR_DELIMITER = len( REAR_DELIMITER ) # get zipcode of interest reply = input( 'Enter zipcode: ' ) zipcode = reply.strip() # clean-up response # specify complete query query_link = WEATHER_GOV_QUERY + zipcode # get response from weather.gov text = url.get_text( query_link ) # to get the forecast, itself, we need to find the forecast # start by finding the forecast delimiters front_index = text.find( FRONT_DELIMITER ) # find starting index of # FRONT_DELIMITER rear_index = text.find( REAR_DELIMITER, front_index ) # find starting index of # READ_DELIMITER. However, # want the occurrence that # follows the FRONT_DELIMITER forecast = text[ front_index : rear_index ] # print what we got for region # of interest print( forecast ) print() # get the indices for the front and rear of the forecast forecast_start = front_index + LENGTH_FRONT_DELIMITER # forecast starts right beyond the # front delimiter forecast_rear = rear_index - 1 # end of the forecast is immediately # before rear delimiter # ready to get and print the forecast forecast = text[ forecast_start : forecast_rear + 1 ] # remember when slicing -- the end is # 1 index after what you want up to # print the forecast print( forecast )