Skip to content

Instantly share code, notes, and snippets.

Created February 19, 2010 17:05
What would you like to do?
## get the weather condition (rainy, cloudy) for any given city
## by @limitedmage
import urllib2
def getWeather(city):
#create google weather api url
url = "" + urllib2.quote(city)
# open google weather api url
f = urllib2.urlopen(url)
# if there was an error opening the url, return
return "Error opening url"
# read contents to a string
s =
# 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: ")
weather = getWeather(city)
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment