On Habré there is an interesting article about how
enthusiasts make the weather . Enthusiasts do, and we use the fruits of their labors - we get this very weather from OpenWeatherMap.org script in Python.
To access the weather service, you will have to go through a simple registration procedure on the
OpenWeatherMap.org website. We will create and send a request, sort the response packet in
JSON format , and get the current temperature with a description of the weather conditions.
Registering for
openweathermap.org is a snap, and the rest will be made even easier.
')
Registration is needed to obtain an identifying user App Id string, consisting of a set of letters and numbers (it looks like hexadecimal numbers only). This type:
"6d8e495ca73d5bbc1d6bf8ebd52c4". After registration, you need to go to your personal account and get the App Id, which is called the “API key” there.
Query string generation
At first we will try to find the city we are interested in in their database. The query string should be something like this:
http://api.openweathermap.org/data/2.5/find?q=Petersburg&type=like&APPID=6d8e495ca73d5bbc1d6bf8ebd52c4
In the request, you need to specify the desired city (instead of “Petersburg”) and your App Id (instead of “6d8e495ca73d5bbc1d6bf8ebd52c4.”
http://api.openweathermap.org/data/2.5/find?q=Petersburg,RU&type=like&APPID=6d8e495ca73d5bbc1d6bf8ebd52c4
The query string itself will be generated by the requests library itself in the get function, which we use to send the query:
requests.get("http://api.openweathermap.org/data/2.5/find", params={'q': s_city, 'type': 'like', 'units': 'metric', 'APPID': appid})
Checking the presence in the database of information on the desired village
The plan is. In response to the generated request, we receive the package in JSON format. Parse the package and get the desired values by the field names.
import requests s_city = "Petersburg,RU" city_id = 0 appid = "- APPID" try: res = requests.get("http://api.openweathermap.org/data/2.5/find", params={'q': s_city, 'type': 'like', 'units': 'metric', 'APPID': appid}) data = res.json() cities = ["{} ({})".format(d['name'], d['sys']['country']) for d in data['list']] print("city:", cities) city_id = data['list'][0]['id'] print('city_id=', city_id) except Exception as e: print("Exception (find):", e) pass
Let us remember the numeric city identifier city_id for the subsequent query, because the service providers recommended doing the query not by name, but by identifier.
The answer may be several cities that match our request. By the way, if you specify “Moscow” in the request and remove the country from the line in the example request, then we are guaranteed to get several lines in the cities list:
city: ['Moscow (RU)', 'Moscow (US)', 'Moscow (US)']
Get current weather information
It remains only to obtain the desired information about the weather. If we are not interested in imperial units, then in the request we indicate that we want to get the metric units: "units = metric". If the weather description needs to be received in Russian, then we specify "lang = ru".
try: res = requests.get("http://api.openweathermap.org/data/2.5/weather", params={'id': city_id, 'units': 'metric', 'lang': 'ru', 'APPID': appid}) data = res.json() print("conditions:", data['weather'][0]['description']) print("temp:", data['main']['temp']) print("temp_min:", data['main']['temp_min']) print("temp_max:", data['main']['temp_max']) except Exception as e: print("Exception (weather):", e) pass
If you believe the service, now (11/14/2016 at 23:20) in Moscow:
conditions: light snow
temp: -5.25
temp_min: -6
temp_max: -5
5 day forecast
try: res = requests.get("http://api.openweathermap.org/data/2.5/forecast", params={'id': city_id, 'units': 'metric', 'lang': 'ru', 'APPID': appid}) data = res.json() for i in data['list']: print( i['dt_txt'], '{0:+3.0f}'.format(i['main']['temp']), i['weather'][0]['description'] ) except Exception as e: print("Exception (forecast):", e) pass
We get this conclusion:
2016-11-24 15:00 -1 7 /
2016-11-24 18:00 +2 7 /
2016-11-24 21:00 +2 7 /
2016-11-25 00:00 -0 7 /
2016-11-25 03:00 +0 7 /
2016-11-25 06:00 -0 6 /
...
Download
owm-request.py . In order for this script to work, you need to enter your “API key” in the first line, received when registering at OpenWeatherMap.org.
The command line, for example, is:
$python owm-request.py Moscow,RU
On the site OpenWeatherMap there is still a lot of interesting things - obtaining information on geographical coordinates, a weather archive, information from specific weather stations. Description of all available services can be found here
http://openweathermap.org/api
There is a specialized
pyowm library for working on Python with OpenWeatherMap.
In addition to OpenWeatherMap, there are other sites that provide similar information. For example,
WorldWeatherOnline . Available APIs can be viewed
here . Registration is required. There is a Python library:
pywwo .
Special thanks to
JetBrains for
PyCharm .