Get upcoming weather from python weather api

Code and results

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
**************************

Commentary

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

Get upcoming weather from python weather api
Use e-Stat API from Python
[Python] Get the text of the law from the e-GOV Law API
Get schedule from Garoon SOAP API with Python + Zeep
Get your heart rate from the fitbit API in Python!
Get data from Quandl in Python
Get reviews with python googlemap api
Get the weather with Python requests
Get the weather with Python requests 2
Use kabu Station® API from Python
Use the Flickr API from Python
Get data from analytics API with Google API Client for python
Run Ansible from Python using API
Get weather information with Python & scraping
Use Google Analytics API from Python
Handle SOAP API from Python (Zeep)
How to get followers and followers from python using the Mastodon API
Get US stock price from Python with Web API with Raspberry Pi
Collecting information from Twitter with Python (Twitter API)
[Python] Web application from 0! Hands-on (3) -API implementation-
Get html from element with Python selenium
Get exchange rates from open exchange rates in Python
[Note] Get data from PostgreSQL with Python
Get keystrokes from / dev / input (python evdev)
Python: Reading JSON data from web API
Get Google Fit API data in Python
Get Youtube data in Python using Youtube Data API
Use Google Cloud Vision API from Python
I tried using UnityCloudBuild API from Python
Get battery level from SwitchBot in Python
[Python] Get Python package information with PyPI API
Get Precipitation Probability from XML in Python
API explanation to touch mastodon from python
[Python] Get the main color from the screenshot
Connect to coincheck's Websocket API from Python
Get metric history from MLflow in Python
Golang api get
MeCab from Python
Get time series data from k-db.com in Python
Get the contents of git diff from python
Get the weather in Osaka via WebAPI (python)
[Python] Get all comments using Youtube Data API
Get stock price data with Quandl API [Python]
[Bash] Use here-documents to get python power from bash
Get BTC / JPY board information from Python --bitflyer
[Python] Get one year's message history from Slack
Get Gmail subject and body with Python and Gmail API
Get only articles from web pages in Python
Try accessing the YQL API directly from Python 3
Get users belonging to your organization from Garoon REST API with Python + Requests
Use thingsspeak from python
[Python] Get environment variables
Touch MySQL from Python 3
[First API] Try to get Qiita articles with Python
Operate Filemaker from Python
Translator in Python from Visual Studio 2017 (Microsoft Translator Text API)
Use fluentd from python
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
[Python] Get Qiita trends