Python 使用 feedparser 解析 Yahoo 天气 RSS
我正在尝试使用feedparser从雅虎天气的RSS获取一些数据。看起来feedparser把yweather这个命名空间的数据给去掉了:
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" />
看起来feedparser完全忽略了这些数据。有没有办法获取到它呢?
2 个回答
0
为了完整性,feedparser 也支持这个功能。一般的写法是用命名空间前缀加下划线再加标签名(比如,yweather_condition)。
在给出的雅虎天气示例中,可以这样做:
import feedparser
d=feedparser.parse('http://weather.yahooapis.com/forecastrss?w=24260013&u=c')
print (d['items'][0]['yweather_condition'])
结果是
{'date': u'Mon, 18 Jul 2011 7:53 pm EDT', 'text': u'Fair', 'code': u'34', 'temp': u'27'}
相关文档可以在这里找到:http://www.feedparser.org/docs/namespace-handling.html
0
这里有一种方法可以使用 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')]
关于 在使用命名空间时的xpath 这一部分可能会特别有帮助。