Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

41 lines (29 sloc) 1.029 kb
## get the weather condition (rainy, cloudy) for any given city
## by @limitedmage julianapena.com
import urllib2
def getWeather(city):
#create google weather api url
url = "http://www.google.com/ig/api?weather=" + urllib2.quote(city)
try:
# open google weather api url
f = urllib2.urlopen(url)
except:
# if there was an error opening the url, return
return "Error opening url"
# read contents to a string
s = f.read()
# extract weather condition data from xml string
weather = s.split("<current_conditions><condition data=\"")[-1].split("\"")[0]
# if there was an error getting the condition, the city is invalid
if weather == "<?xml version=":
return "Invalid city"
#return the weather condition
return weather
def main():
#while True:
# city = raw_input("Give me a city: ")
print "Weather report in chennai"
weather = getWeather('madras')
print(weather)
if __name__ == "__main__":
main()
Jump to Line
Something went wrong with that request. Please try again.