Get Precipitation Probability from XML in Python

Parent article: Making an umbrella reminder with Raspberry Pi Zero W

As the title says. I'm new to Python and don't understand XML yet. I will study one by one. The version of Python I'm using is 2.7.9.

Link that was taken care of

http://www.drk7.jp/weather/ It seems that the weather forecast information released by the Japan Meteorological Agency is distributed in XML format. Thank you for using it.

https://docs.python.jp/2.7/library/xml.etree.elementtree.html How to parse XML.

The finished code

Displays the probability of precipitation in eastern Yokohama. I want to know mainly the probability of precipitation in the evening and at night, so

--12:00 --18:00 --18:00 --24:00

It is divided into two parts. I feel that there seems to be a more efficient way. Please teach me.

2017/08/13 Addendum: In the comment section, there is a cleaner code using the inclusion notation that @shiracamus taught me.

code


# coding: utf_8

import datetime
today = datetime.datetime.today().strftime("%Y/%m/%d")

import requests
url = 'http://www.drk7.jp/weather/xml/14.xml'
response = requests.get(url)

import xml.etree.ElementTree as ET
root = ET.fromstring(response.content)

# Get rainfallchance of North Yokohama
# Time: 12h - 18h and 18h - 24h

for area in root.iter('area'):
    if area.get('id').encode('utf_8') == 'Eastern':
        for info in area.findall('info'):
            if info.get('date') == today:
                rainfallchance = info.find('rainfallchance')
                for period in rainfallchance.findall('period'):
                    hour = period.get('hour')
                    if hour == '12-18' or hour == '18-24':
                        print hour + 'h ' + period.text + '%'

result


12-18h 10%
18-24h 20%

Recommended Posts

Get Precipitation Probability from XML in Python
Get data from Quandl in Python
Get exchange rates from open exchange rates in Python
Get battery level from SwitchBot in Python
Get metric history from MLflow in Python
Parse XML in Python
Get date in Python
Get time series data from k-db.com in Python
Get only articles from web pages in Python
Get last month in python
OCR from PDF in Python
Get data from GPS module at 10Hz in Python
Read Namespace-specified XML in Python
Get Terminal size in Python
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!
How to sample from any probability density function in Python
Hit REST in Python to get data from New Relic
Get macro constants from C (++) header file (.h) in Python
Get message from first offset with kafka consumer in python
Get Leap Motion data in Python.
Get the desktop path in Python
Get the script path in Python
Extract text from images in Python
Get, post communication memo in Python
Get upcoming weather from python weather api
Get the desktop path in Python
Get the host name in Python
Get started with Python in Blender
Extract strings from files in Python
How to get a string from a command line argument in python
Get additional data in LDAP with python
Get html from element with Python selenium
[Note] Get data from PostgreSQL with Python
Get Suica balance in Python (using libpafe)
Get keystrokes from / dev / input (python evdev)
Revived from "no internet access" in Python
Prevent double boot from cron in Python
Markov chain transition probability written in Python
Get Google Fit API data in Python
Get Youtube data in Python using Youtube Data API
Download images from URL list in Python
Get a token for conoha in python
Generate a class from a string in Python
Generate C language from S-expressions in Python
Get the EDINET code list in Python
Convert from Markdown to HTML in Python
Get Cloud Logging available in Python in 10 minutes
Manipulate namespaced XML in Python (Element Tree)
[Python] Get the main color from the screenshot
Get rid of DICOM images in Python
Get your own IP address in Python
Get options in Python from both JSON files and command line arguments
Quadtree in Python --2
CURL in python
Studying Mathematics in Python: Solving Simple Probability Problems
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python