Let’s learn how to use yweather.

Create a Client Object

yweather consists of a single class, Client.

>>> import yweather
>>> client = yweather.Client()

By creating an instance of Client, you’ve created an object that you can use to fetch location identifiers and weather data from Yahoo! Weather.

Fetch a Location’s WOEID

Yahoo! Weather gives every location a unique WOEID. In order to fetch weather data from Yahoo!, you must first know the location’s WOEID.

>>> client.fetch_woeid("Beijing, China")
>>> client.fetch_woeid("96734")
>>> client.fetch_woeid("10 South Main Street, Harrisonburg, VA")

You can retrieve a WOEID by passing a general or specific address. The above example used city and country, ZIP code, and complete address.

Fetch a Location’s Weather

Once you have a location’s WOEID, you can use it to fetch the location’s weather. Weather data is returned as a dict. Its structure is detailed in fetch_weather()‘s API documentation.

>>> beijing_weather = client.fetch_weather("2151330")
>>> beijing_weather["guid"]
>>> beijing_weather["description"]
'Yahoo! Weather for Beijing, CN'
>>> beijing_weather["condition"]["temp"]

The returned dict contains metadata along with the weather data itself. By default, United States customary units are used, but by changing the metric argument, you can receive data according to the metric system.

>>> kailua_weather = client.fetch_weather("12798281", metric=True)
>>> kailua_weather["forecast"][0]["high"]
>>> kailua_weather["units"]["forecast"]["high"]

The units used for each data value are accessible with the units key.

Using a Location’s LID

Because Yahoo! Weather’s data comes from The Weather Channel, weather data is also accessible via a The Weather Channel LID. This provides access to a 5-day forecast versus the 2-day forecast available with a location’s WOEID.

>>> client.fetch_lid("2151330")
>>> beijing_weather = client.fetch_weather("CHXX0008")
>>> len(beijing_weather["forecast"])

The fetch_lid() method takes a WOEID and returns a LID. You can pass the LID to the fetch_weather() method.

Project Versions

Table Of Contents

Previous topic


Next topic


This Page