''' 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 Note: makes use of a csv web file adapted from www.boutell.com/zipcodes/ ''' # get url access capabilities import urllib.request # define weather.gov base query WEATHER_GOV_QUERY = 'http://forecast.weather.gov/zipcity.php?inputstring=' # get zipcode of interest zipcode = input( 'Enter zipcode: ' ) # specify complete query query = WEATHER_GOV_QUERY + zipcode # get a connection to the web resource of interest stream = urllib.request.urlopen( query ) # read stream to get its contents contents = stream.read() # decode contents into plain text form page = contents.decode( 'UTF-8' ) # forecast delimiters OPENING_DELIMITER = 'forecast-text">' # this string is in front of the forecast that we actually want CLOSING_DELIMITER = '<' # this string is after the forecast we actually want # to get the forecast, itself, we need to find the forecast i = page.index( ... ) j = page.index( ... ) forecast = page[ i : j ] print(forecast)