[Tutor] possibly a version error
Michael Harleman
m_harleman at hotmail.com
Fri Dec 23 23:52:40 CET 2011
> Date: Fri, 23 Dec 2011 14:04:17 +0100
> From: jugurtha.hadjar at gmail.com
> To: tutor at python.org
> Subject: Re: [Tutor] possibly a version error
>
> On 22/12/2011 18:13, Cranky Frankie wrote:
> > 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))
> >
>
> Hello,
>
> I tried it and it works fine (Python 3.2, Windows XP (5.1.2600) )
>
> Here's what I got:
>
> {'current_condition': 'Light Rain',
> 'current_temp': '37',
> 'forecasts': [{'condition': 'AM Rain/Snow',
> 'date': '23 Dec 2011',
> 'high': '39',
> 'low': '16'},
> {'condition': 'Partly Cloudy',
> 'date': '24 Dec 2011',
> 'high': '31',
> 'low': '20'}],
> 'title': 'Yahoo! Weather - Schenectady, NY'}
>
>
> I'll probably tinker with it to make it show the weather here in Algiers
> (Algeria, North Africa).
>
> Thanks,
>
>
> --
> ~Jugurtha Hadjar,
I'm a newbie, but I played around with your code to see if I could get
it to work in 2.7 and it required changing the same line to:
dom = minidom.parse(urllib.URLopener().open(url))
Just in case someone here were to find that helpful.
Mike Harleman
More information about the Tutor
mailing list