我正在尝试使用feedparser从yahoos天气rss获取一些数据。它看起来像是提要解析器剥离了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完全忽略了这一点。有没有办法拿到它?
发布于 2010-05-20 10:55:28
以下是使用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')]关于using xpath with namespaces的部分可能特别有帮助。
发布于 2011-07-19 09:24:46
为了完整性,feedparser也支持这一点。一般语法是命名空间前缀下划线标记名称(例如,yweather_condition)。
在给出的Yahoo天气示例中,可以这样做:
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上
https://stackoverflow.com/questions/2870258
复制相似问题