python feedparser with yahoo weather rss
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:开发者_如何学Pythoncondition 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?
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&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.
For completeness, feedparser DOES suppport this as well. The general syntax is namespace prefix underscore tag name (e.g., yweather_condition).
In the Yahoo weather example given, one can do:
import feedparser
d=feedparser.parse('http://weather.yahooapis.com/forecastrss?w=24260013&u=c')
print (d['items'][0]['yweather_condition'])
yields
{'date': u'Mon, 18 Jul 2011 7:53 pm EDT', 'text': u'Fair', 'code': u'34', 'temp': u'27'}
The documentation is at http://www.feedparser.org/docs/namespace-handling.html
精彩评论