1

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

Los Angeles Weather

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"/>

I want to print something like:

Sunrise: 6.20 am
Sunset: 4.52 pm

2 Answers 2

3

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']
Sign up to request clarification or add additional context in comments.

Comments

2

Use feedparser:

import feedparser

feed = feedparser.parse('http://weather.yahooapis.com/forecastrss?w=2442047&u=c')
sunrise = feed.items()[0][1]["yweather_astronomy"]["sunrise"]
sunset = feed.items()[0][1]["yweather_astronomy"]["sunset"]

Play with the result of parse to get a feeling for the structure of the parsed feed.

1 Comment

@sarbo But the answer by James Henstridge is even better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.