Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

"""Retrieve the weather report for the current location."""

__author__ = 'T.V. Raman '
__copyright__ = 'Copyright (c) 2009, Google Inc.'
__license__ = 'Apache License, Version 2.0'

import string
import urllib.request, urllib.parse, urllib.error
import urllib.request, urllib.error, urllib.parse
from xml.dom import minidom


def extract_value(dom, parent, child):
"""Convenience function to dig out weather values."""
return dom.getElementsByTagName(parent)[0].getElementsByTagName(child)[0].getAttribute('data')

def fetch_weather(location, hl=''):
"""Fetches weather report from Google

location: a zip code (94041); city name, state (weather=Mountain View,CA);...
hl: the language parameter (language code)

a dict of weather data.

url = WEATHER_URL % (urllib.parse.quote(location), hl)
handler = urllib.request.urlopen(url)
data =
dom = minidom.parseString(data)

data = {}
weather_dom = dom.getElementsByTagName('weather')[0]
data['city'] = extract_value(weather_dom, 'forecast_information','city')
data['temperature'] = extract_value(weather_dom, 'current_conditions','temp_f')
data['conditions'] = extract_value(weather_dom, 'current_conditions', 'condition')
return data

This post first appeared on Ricky's Python Notes, please read the originial post: here

Share the post


Subscribe to Ricky's Python Notes

Get updates delivered right to your inbox!

Thank you for your subscription