views:

76

answers:

1

I'm trying to use feedparser to get some data from yahoos weather rss. It looks like feed parser strips out the yweather namespace data:

http://weather.yahooapis.com/forecastrss?w=24260013&u=c

<yweather:condition  text="Fair" code="34"  temp="23"  date="Wed, 19 May 2010 5:55 pm EDT" />

looks like feedparser is completely ignoring that. is there away to get it?

A: 

Here is one way you could get the data using using lxml:

import urllib2
import lxml.etree

url = "http://weather.yahooapis.com/forecastrss?w=24260013&amp;u=c"
doc = lxml.etree.parse( urllib2.urlopen(url) ).getroot()
conditions = doc.xpath('*/*/yweather:condition',
                       namespaces={'yweather': 'http://xml.weather.yahoo.com/ns/rss/1.0'})
try:
    condition=conditions[0]
except IndexError:
    print('yweather:condition not found')
print(condition.items())
# [('text', 'Fair'), ('code', '33'), ('temp', '16'), ('date', 'Wed, 19 May 2010 9:55 pm EDT')]

The section on using xpath with namespaces might be particularly helpful.

unutbu