1

私はfeedparserを使用してyahoosweatherrssからいくつかのデータを取得しようとしています。フィードパーサーが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はそれを完全に無視しているようです。それを手に入れるために離れていますか?

4

2 に答える 2

0

lxmlを使用してデータを取得する1つの方法は次のとおりです。

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の使用に関するセクションは、特に役立つ場合があります。

于 2010-05-20T02:55:27.683 に答える
0

完全を期すために、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にあります

于 2011-07-19T01:24:46.203 に答える