""" Purpose: provide weather forecast by accessing US Weather Service Usage: user provides a zipcode Output: current forecast for that zipcode """ # get access to web-based data support import url # set base weather.gov query WEATHER_GOV_QUERY = "https://forecast.weather.gov/zipcity.php?inputstring=" # base query is going to be the root of every link that we create, modified by different zip codes # get zipcode of interest reply = input( "Enter zipcode: " ) zipcode = reply.strip() # clean it up so it doesn't get mad at us if we try to put spaces in the link # set complete query link link = WEATHER_GOV_QUERY + zipcode # builds a link out of query base and relevant zip code # this is not a folder/file system, it's a website that runs specific programs # when you log into collab you can see something=True, this also uses a function to see if you're logged in # query weather.gov page = url.get_contents( link ) # we got the contents, but we don't want that huge awful string, we only want some # need to find the forecast within the page # forecast delimiters FRONT_DELIMITER = "
" # text preceding forecast REAR_DELIMITER = "
" # text following forecast # delimiters tell us where we can find our forecast. delimeters surround the text of interest # will we ask you to find the pattern? no, the pattern will be given and you'll use string methods to find() # the occurrences # delimiter lengths LENGTH_FRONT_DELIMITER = len( FRONT_DELIMITER ) LENGTH_REAR_DELIMITER = len( REAR_DELIMITER ) # we don't use this in this program! # start by finding the forecast delimiters front_index = page.find( FRONT_DELIMITER ) # this tells us when the pattern starts, not where it ends rear_index = page.find( REAR_DELIMITER, front_index ) # there are a bunch of comments in html code, so we have to look for the first one after the start # of the front delimiter to get the forecast. the second input gives the starting index for the search forecast = page[ front_index : rear_index ] # [i:j] slicing/substrings - start at i and go to j-1 print( forecast ) # account for length of front delimiter, as the forecast starts directly after it forecast_start = front_index + LENGTH_FRONT_DELIMITER # get rid of that yucky front delimiter and only give us our forecast print( "front_index:", front_index ) print( "forecast_start:", forecast_start ) print( "rear_index:", rear_index ) # get and print the forecast forecast = page[ forecast_start : rear_index ] # [i:j] slicing/substrings - start at i and go to j-1 print( zipcode, "forecast:", forecast )