Get the weather in Osaka via WebAPI (python)

Purpose

It is a memorandum when the weather in Osaka was acquired via WebAPI (python).

Preparation

Please refer to the following. For weather information, please refer to the website of www.drk7.jp.

Distribute the weather forecast information of the Japan Meteorological Agency in XML Getting Precipitation Probability from XML in Python

code

weather.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import datetime
import requests
import xml.etree.ElementTree as ET

def chance_of_rain(pref_url, date):
    if date is None:
        date = datetime.datetime.today().strftime("%Y/%m/%d")
    root = ET.fromstring(requests.get(pref_url).content)
    for area in root.iter('area'):
            for info in area.findall('info'):
                if info.get('date') == date:
                    rainfallchance = info.find('rainfallchance')
                    for period in rainfallchance.findall('period'):
                        yield period.get('hour'), period.text

if __name__ == '__main__':
    prefecture = 'http://www.drk7.jp/weather/xml/27.xml'
    hours = '00-06', '06-12', '12-18', '18-24'
    date = datetime.datetime.today().strftime("%Y/%m/%d")
    print('Precipitation probability in Osaka', date)
    for hour, percent in chance_of_rain(prefecture, date):
        if hour in hours:
            print('%sh %s%%' % (hour, percent))
$ python weather.py 
Precipitation probability in Osaka 2019/08/13
00-06h 0%
06-12h 0%
12-18h 10%
18-24h 10%

How can I visualize it in a nice way?

reference

Distribute the weather forecast information of the Japan Meteorological Agency in XML Getting Precipitation Probability from XML in Python A story that took a lot of time to get the probability of precipitation [Python] raspberry pi Added weather forecast to table clock Try using Tkinter in Python

Recommended Posts

Get the weather in Osaka via WebAPI (python)
Get the desktop path in Python
Get the weather with Python requests
Get the weather with Python requests 2
Get the script path in Python
Get the desktop path in Python
Get the host name in Python
Get the EDINET code list in Python
[Python] Get the files in a folder with Python
Get the caller of a function in Python
Get the X Window System window title in Python
Get date in Python
How to get the files in the [Python] folder
How to get the variable name itself in python
How to get the number of digits in Python
[Python] Get the numbers in the graph image with OCR
[python] Get the list of classes defined in the module
Get the result in dict format with Python psycopg2
Get the size (number of elements) of UnionFind in Python
Get the value selected in Selenium Python VBA pull-down
Get the URL of the HTTP redirect destination in Python
Get last month in python
Download the file in Python
Find the difference in Python
Get Terminal size in Python
[Python] Get the previous month
Explicitly get EOF in python
Get Evernote notes in Python
Get Japanese synonyms in Python
Get your heart rate from the fitbit API in Python!
Get the MIME type in Python and determine the file format
Get the number of specific elements in a python list
Get the value while specifying the default value from dict in Python
How to get the last (last) value in a list in Python
Automatically get the port where Arduino is stuck in Python
Get the current date and time in Python, considering the time difference
Get the index of each element of the confusion matrix in Python
Get Leap Motion data in Python.
Getting the arXiv API in Python
Python in the browser: Brython's recommendation
Get data from Quandl in Python
Save the binary file in Python
Hit the Sesami API in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
How to get the Python version
Get, post communication memo in Python
Hit the web API in Python
Get upcoming weather from python weather api
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python
Access the Twitter API in Python
Python Note: Get the current month
The first step in Python Matplotlib
Get started with Python in Blender
Get weather information with Python & scraping
I wrote the stack in Python
Get the query string (query string) in Django
Master the weakref module in Python
Get the Ticker Symbol for US exchange listed stocks in Python