如何从 Python 中的 RSS 提要获取 XML 属性和值? [英] How can I get XML attributes and values from an RSS feed in Python?

查看:40
本文介绍了如何从 Python 中的 RSS 提要获取 XML 属性和值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Yahoo!用于检索某些值的天气 API.这是请求:

I'm using the Yahoo! Weather API to retrieve some values. This is the request:

洛杉矶天气

如何从以下获取属性和值:

How can I get attributes and values from the following:

<yweather:astronomy sunrise="6:20 am" sunset="4:52 pm"/>
<yweather:condition text="Partly Cloudy" code="29" temp="14" date="Fri, 09 Nov 2012 1:47 am PST"/>

我想打印如下内容:

Sunrise: 6.20 am
Sunset: 4.52 pm

推荐答案

您可以使用 feedparser 库来做到这一点:

You can do this using the feedparser library:

import feedparser

feed = feedparser.parse('http://weather.yahooapis.com/forecastrss?w=2442047&u=c')
print 'Sunrise:', feed.feed.yweather_astronomy['sunrise']
print 'Sunset:', feed.feed.yweather_astronomy['sunset']

这篇关于如何从 Python 中的 RSS 提要获取 XML 属性和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆