[Tutor] possibly a version error
Jugurtha Hadjar
jugurtha.hadjar at gmail.com
Fri Dec 23 14:04:17 CET 2011
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,
More information about the Tutor
mailing list