[Tutor] possibly a version error

Cranky Frankie cranky.frankie at gmail.com
Thu Dec 22 18:13:19 CET 2011


I got it to work:

Use this for the import - import urllib.request

the use this: dom = minidom.parse(urllib.request.urlopen(url))

Here's the code that works in 3.2:

from pprint import pprint
import urllib.request
from xml.dom import minidom

WEATHER_URL = 'http://xml.weather.yahoo.com/forecastrss?p=%s'
WEATHER_NS = 'http://xml.weather.yahoo.com/ns/rss/1.0'

def weather_for_zip(zip_code):
    url = WEATHER_URL % zip_code
    dom = minidom.parse(urllib.request.urlopen(url))
    forecasts = []
    for node in dom.getElementsByTagNameNS(WEATHER_NS, 'forecast'):
        forecasts.append({
            'date': node.getAttribute('date'),
            'low': node.getAttribute('low'),
            'high': node.getAttribute('high'),
            'condition': node.getAttribute('text')
        })
    ycondition = dom.getElementsByTagNameNS(WEATHER_NS, 'condition')[0]
    return {
        'current_condition': ycondition.getAttribute('text'),
        'current_temp': ycondition.getAttribute('temp'),
        'forecasts': forecasts,
        'title': dom.getElementsByTagName('title')[0].firstChild.data
    }

pprint(weather_for_zip(12303))

-- 
Frank L. "Cranky Frankie" Palmeri


More information about the Tutor mailing list