Masthead

Weather Service Example

Below is an example of accessing a weather service and obtaining the prediction based on a ZIP code. This example was created by Ivan Dane.

def XMLparser (TheXML):

    Key1=""
    Key2=""
    Key3 = ""
    Key4 = ""
    Key5 = ""
    Key6 = ""    
    
    LengthOfKey1 = len(Key1)
    LengthOfKey2 = len(Key2) 
    LengthOfKey3 = len(Key3)
    LengthOfKey4 = len(Key4)
    LengthOfKey5 = len(Key5)
    LengthOfKey6 = len(Key6)
    
    
    StartIndex = 0 #Initialize StartIndex so we go into the loop the first time
    WeatherIndex = 0
    TempIndex = 0

    try:
        while (StartIndex!=-1):
        
            StartIndex=TheXML.find(Key1,StartIndex) # Find the start of the coordinate tag
        
            if (StartIndex!=-1): # Only extract the coordinate if one was found
        
                EndIndex=TheXML.find(Key2,StartIndex+LengthOfKey1) # Get the end of the coordinate tag
        
                CityValue=TheXML[StartIndex+LengthOfKey1:EndIndex] # Extract the coordinate from between the 'pos' tags
        
                StartIndex=EndIndex+LengthOfKey2 # Move StartIndex to be after the end key to find the next entry
                WeatherIndex = TheXML.find(Key3, StartIndex) #Identifies character position of the tide height value tag
                EndWeatherIndex = TheXML.find(Key4,WeatherIndex+LengthOfKey4) #Identifies 
                WeatherValue = TheXML[WeatherIndex+LengthOfKey3:EndWeatherIndex] #Extracts tide height value from between value tags
        
                StartIndex=EndWeatherIndex+LengthOfKey4 # Move StartIndex to be after the end key to find the next entry
                TempIndex = TheXML.find(Key5, StartIndex) #Identifies character position of the tide height value tag
                
                if (TempIndex<>-1):
                    EndTempIndex = TheXML.find(Key6,WeatherIndex+LengthOfKey4) #Identifies 
                    TempValue = TheXML[TempIndex+LengthOfKey5:EndTempIndex] #Extracts             
                else: 
                    TempValue = "N/A"
                
                DataString = CityValue + "," + WeatherValue + "," + TempValue
                
                print(CityValue + "," + WeatherValue) #compiles the Lat, Long, and tide value into CSV format and writes it to the CSV file 
    
             
        return(DataString)  #TheOutputFile.write(DataString) #Writes the fdata to the file
    except:
        DataString = "N/A" + "," + "N/A" + "," + "N/A"
        return(DataString)
##########################################################

import urllib2 # Include the URL library

TheOutputFile = open("C:/temp/WeatherByZip.csv","w")

ZIP = '95501' #manually enter zip code here

TheService=urllib2.urlopen('http://wsf.cdyne.com/weatherws/Weather.asmx/GetCityWeatherByZIP?ZIP=' + ZIP) # Open the URL
TheHTML=TheService.read() 

DataValues = XMLparser (TheHTML)

TheOutputFile.write("City" + "," + "Weather" + "," + "Temperature" +"\n") #Writes the file headers to the CSV file
TheOutputFile.write(DataValues) #Writes the file headers to the CSV file

TheOutputFile.close()


 

© Copyright 2018 HSU - All rights reserved.