Note: This snippet uses piksemel module to parse XML files. If you don’t have it, read this article to install it.
First of all, you need the WOEID (Where on Earth Identification) of the city. In order to learn it, go to http://weather.yahoo.com and search for the city. You’ll find the WOEID at the end of the url of the page.
# for Istanbul, TR woeid = 2344116
Use this id in Yahoo! Weather API url, for w parameter.
weatherUri = "http://weather.yahooapis.com/forecastrss?w=%d" % woeid
If you want the results to be in metric units, add u=c parameter into your query.
weatherUri = "http://weather.yahooapis.com/forecastrss?w=%d&u=c" % woeid
Using urllib, fetch the XML file:
import urllib xml = urllib.urlopen(weatherUri).read()
We can parse this XML easily using piksemel:
import piksemel temp = piksemel.parseString(xml).getTag('channel').getTag('item').getTag('yweather:condition').getAttribute('temp')
You can also view XML file and look for other resources such as: humidity, visibility, wind, etc.