This time, livedoor livedoor weather information | weather information provided by livedoor Bring information from the api. By default, the weather information of Kagoshima is fetched. If you give the code of another area to the argument, the weather information of that area will be fetched. For the code of each region, refer to the id of each region at the URL below. http://weather.livedoor.com/forecast/rss/primary_area.xml
weather_api.py
#encoding:utf-8
import urllib2, sys
import json
try: citycode = sys.argv[1]
except: citycode = '460010' #Default region
resp = urllib2.urlopen('http://weather.livedoor.com/forecast/webservice/json/v1?city=%s'%citycode).read()
#Convert the read JSON data to dictionary type
resp = json.loads(resp)
print '**************************'
print resp['title']
print '**************************'
print resp['description']['text']
for forecast in resp['forecasts']:
print '**************************'
print forecast['dateLabel']+'('+forecast['date']+')'
print forecast['telop']
print '**************************'
When executed (as of 2015-06-29)
$ python weather_api.py
**************************
Kagoshima Prefecture Kagoshima Weather
**************************
The southern part of Kyushu is mostly sunny.
In the Amami region, there are places where it is raining heavily.
Southern Kyushu will be covered with high pressure on the 29th, but gradually the trough of pressure and
Will be affected by moist air. On the 30th, it will be affected by the Baiu front
It is included.
The Amami region will be affected by the Baiu front on the 29th. 30th is plum
Expected to be affected by rain fronts and moist air.
Southern Kyushu will be sunny on the 29th and cloudy at night. 30th is the beginning
It is cloudy, but it will gradually rain, and it is expected that there will be places where it will rain very hard with lightning.
I will.
In the Amami region, there will be places where it will rain very hard with thunder on the 29th.
.. On the 30th, there are places where it rains heavily with lightning, but it gradually becomes cloudy.
It is included.
In the Amami region, high temperatures are expected on the 29th, so pay attention to health management such as heat stroke.
Please to mind.
At sea, on the 29th, the waves will be a little high or some waves. 30th
, It is expected that there will be places where the waves will gradually rise.
Mt. Kirishima(Shinmoedake)Wind about 1500 meters above the sky
29th 09:00 Northeast wind 3 meters
Forecast of 21:00 on the 29th East wind 2 meters
Wind of about 1500 meters above Sakurajima
29th 09:00 East wind 5 meters
Forecast of 21:00 on the 29th Southeast wind 2 meters
<Points to keep in mind such as weather changes>
In Yakushima Town, it will be mostly cloudy on the 29th. 30th is cloudy at first
However, there are places where it rains from morning and it rains very hard with lightning from early afternoon to evening.
There is a prospect.
**************************
today(2015-06-29)
Clear in the morning butterflies
**************************
tomorrow(2015-06-30)
rain
**************************
day after tomorrow(2015-07-01)
Cloudy and sometimes rain
**************************
urllib2 urllib2 is a python module that gets resources on the net from URLs.
import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()
Reference: http://docs.python.jp/2/howto/urllib2.html
In this code,
resp = urllib2.urlopen('http://weather.livedoor.com/forecast/webservice/json/v1?city=%s'%citycode).read()
That's right. Here, we are getting resources from the api of "http://weather.livedoor.com".
sys.argv sys defines functions that are closely related to the operation of the interpreter.
Interpreter: An interpreter is software that executes a program while converting a software design drawing (source code) written in a programming language by a human into a computer-executable format (object code). Interpreter-type languages are slower than compiler-type languages because they are converted when the program is executed. Source: [IT terminology e-Words | Interpreter](http://e-words.jp/w/%E3%82%A4%E3%83%B3%E3%82%BF%E3%83%97 % E3% 83% AA% E3% 82% BF.html)
Especially here, we use sys.argv. This allows you to receive arguments from the command line. Since argv [0] `` `receives the script name, this code uses the first argument
argv [1] `` `.
reference: sys — system parameters and functions Python: Get command line arguments – sys.argv variable
try, except try and except handle error handling. If no exception occurs in the try clause, the except clause is skipped. If an exception occurs, execute the except clause.
This time, if there is a command line argument, give that code to citycode, otherwise give Kagoshima's code to citycode. Reference: 8. Errors and Exceptions
json I'm using a json module to decode the retrieved json.
json.loads()
This time, the received data is converted from json type to dictionary type. 18.2. json — JSON encoder and decoder
Recommended Posts