''' Purpose: provide weather forecast by accessing US Weather Service web service Usage: user provides a zipcode Output: current forecast for that zipcode ''' # get url access capability from cs 1112 module from url import get_contents # define weather.gov base query WEATHER_GOV_QUERY = "https://forecast.weather.gov/zipcity.php?inputstring=" # THIS IS THE BASE URL - WE WILL PROMPT FOR A ZIP CODE AND ADD IT TO THE # END OF OUR WEATHER_GOV_QUERY URL to access weather webpage for a zipcode of # interest # forecast delimiters FRONT_DELIMITER = "
" # text that precedes forecast REAR_DELIMITER = "
" # text that follows forecast # We are going to find the forecast between the front delimiter and # the rear delimiter # 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 # print ( query_link ) # This is us assembling the full link for the # website for the weather webpage of the zip code of interset # get response from weather.gov page = get_contents( query_link ) # print ( page ) -> prints html of the entire webpage # to get the forecast, we need to find it within the page # start by finding the forecast delimiters front_index = page.index( FRONT_DELIMITER ) # WHERE IN MY HUGE HTML CAN I FIND THE FRONT DELIMITER?? (FRONT PART) rear_index = page.index( REAR_DELIMITER, front_index ) # SO THIS IS KEY REMEMBER THAT GET_CONTENTS() GETS YOU # A STRING OF WHATEVER IS ON THAT WEBPAGE SO STRING METHODS # WORK ON WHERE WE STORE WHAT WAS RETURNED BY GET_CONTENTS() # So FRONT_DELIMITER and REAR_DELIMITER SANDWICH THE FORECAST # Find where in the html the front and rear delimiters are # rear_index (first instance of REAR_DELIMITER occuring after # front index) print( front_index, rear_index ) # (mis)try to use those index delimiters to pick off the forecast # This is slicing! Going into text (the string) and getting a slice # (a smaller substring) of the larger string forecast = page[ front_index : rear_index + 1 ] # Get me everything from where the front index is to where the last index # is + 1 so that we include the rear index! # Forecast is the substring of the larger string of the forecast page # from the front_index (where the FRONT_DELIMITER occurs) to the # rear index (where the REAR_DELIMITER occurs) # print( forecast ) print() # get the indices for the front and rear of the forecast forecast_start = front_index + LENGTH_FRONT_DELIMITER forecast_rear = rear_index - 1 # Rear index is where we find the